Class: String

Inherits:
Object
  • Object
show all
Extended by:
FatCore::String::ClassMethods
Includes:
FatCore::String
Defined in:
lib/fat_core/string.rb

Generating collapse

Transforming collapse

Numbers collapse

Matching collapse

Class Method Details

.random(size = 8) ⇒ Object Originally defined in module FatCore::String::ClassMethods

Return a random string composed of all lower-case letters of length size

Instance Method Details

#as_dateDate Originally defined in module FatCore::String

Convert a string representing a date with only digits, hyphens, or slashes to a Date.

Examples:

"20090923".as_date.iso -> "2009-09-23"
"2009/09/23".as_date.iso -> "2009-09-23"
"2009-09-23".as_date.iso -> "2009-09-23"
"2009-9-23".as_date.iso -> "2009-09-23"

Returns:

  • (Date)

    the translated Date

#as_stringString Originally defined in module FatCore::String

Return self unmodified. This method is here so to comply with the API of Symbol#as_string so that it can be applied to a variable that is either a String or a Symbol.

Returns:

  • (String)

    self unmodified

#as_symSymbol Originally defined in module FatCore::String

Convert to a lower-case symbol with all white space converted to a single '_' and all non-alphanumerics deleted, such that the string will work as an unquoted Symbol.

Examples:

"Hello World" -> :hello_world
"Hello*+World" -> :helloworld

Returns:

  • (Symbol)

    self converted to a Symbol

#cleanString Originally defined in module FatCore::String

Remove leading and trailing white space and compress internal runs of white space to a single space.

Examples:

'  hello   world\n  '.clean #=> 'hello world'

Returns:

#commas(places = nil) ⇒ String Originally defined in module FatCore::String

If the string is a valid number, return a string that adds grouping commas to the whole number part; otherwise, return self. Round the number to the given number places after the decimal if places is positive; round to the left of the decimal if places is negative. Pad with zeroes on the right for positive places, on the left for negative places.

Examples:

'hello'.commas             #=> 'hello'
'+4654656.33e66'.commas    #=> '+4,654,656.33e66'
'6789345612.14'.commas(-5) #=> '6,789,350,000'
'6543.14'.commas(5)        #=> '6,543.14000'

Returns:

  • (String)

    self if not a valid number

  • (String)

    commified number as a String

#distance(other) ⇒ Integer Originally defined in module FatCore::String

Return the Damerau-Levenshtein distance between self an another string using a transposition block size of 1 and quitting if a max distance of 10 is reached.

Parameters:

  • other (#to_s)

    string to compute self's distance from

Returns:

  • (Integer)

    the distance between self and other

#entitleString Originally defined in module FatCore::String

Return self capitalized according to the conventions for capitalizing titles of books or articles. Tries to follow the rules of the University of Chicago's A Manual of Style, Section 7.123, except to the extent that doing so requires knowing the parts of speech of words in the title. Also tries to use sensible capitalization for things such as postal address abbreviations, like P.O Box, Ave., Cir., etc. Considers all-consonant words of 3 or more characters as acronyms to be kept all uppercase, e.g., ddt => DDT, and words that are all uppercase in the input are kept that way, e.g. IBM stays IBM. Thus, if the source string is all uppercase, you should lowercase the whole string before using #entitle, otherwise is will not have the intended effect.

Examples:

'now is the time for all good men' #=> 'Now Is the Time for All

Good Men' 'how in the world does IBM do it?'.entitle #=> "How in the
World Does IBM Do It?" 'how in the world does ibm do it?'.entitle #=>
"How in the World Does Ibm Do It?" 'ne by nw'.entitle #=> 'NE by NW' 'my
life: a narcissistic tale' => 'My Life: A Narcissistic Tale'

Returns:

#fuzzy_match(matcher) ⇒ String? Originally defined in module FatCore::String

Return the matched portion of self, minus punctuation characters, if self matches the string matcher using the following notion of matching:

  1. Remove all periods, commas, apostrophes, and asterisks (the punctuation characters) from both self and matcher,
  2. Treat ':' in the matcher as the equivalent of '.*' in a regular expression, that is, match anything in self,
  3. Ignore case in the match
  4. Match if any part of self matches matcher

Examples:

"St. Luke's Hospital".fuzzy_match('st lukes') #=> 'St Lukes'
"St. Luke's Hospital".fuzzy_match('luk:hosp') #=> 'Lukes Hosp'
"St. Luke's Hospital".fuzzy_match('st:spital') #=> 'St Lukes Hospital'
"St. Luke's Hospital".fuzzy_match('st:laks') #=> nil

Parameters:

  • matcher (String)

    pattern to test against where ':' is wildcard

Returns:

  • (String)

    the unpunctuated part of self that matched

  • (nil)

    if self did not match matcher

#matches_with(matcher) ⇒ nil, String Originally defined in module FatCore::String

Test whether self matches the matcher treating matcher as a case-insensitive regular expression if it is of the form '/.../' or as a string to #fuzzy_match against otherwise.

Parameters:

  • matcher (String)

    regexp if looks like /.../; #fuzzy_match pattern otherwise

Returns:

  • (nil)

    if no match

  • (String)

    the matched portion of self, with punctuation stripped in case of #fuzzy_match

See Also:

#number?Boolean Originally defined in module FatCore::String

Return whether self is convertible into a valid number.

Examples:

'6465321'.number?        #=> true
'6465321.271828'.number? #=> true
'76 trombones'           #=> false
'2.77e7'                 #=> true
'+12_534'                #=> true

Returns:

  • (Boolean)

    does self represent a valid number

#tex_quoteString Originally defined in module FatCore::String

Return self with special TeX characters replaced with control-sequences that output the literal value of the special characters instead. It handles _, $, &, %, #, {, }, \, ^, ~, <, and >.

Examples:

'$100 & 20#'.tex_quote #=> '\\$100 \\& 20\\#'

Returns:

#to_regexpRegexp Originally defined in module FatCore::String

Convert a string of the form '/.../Iixm' to a regular expression. However, make the regular expression case-insensitive by default and extend the modifier syntax to allow '/I' to indicate case-sensitive. Without the surrounding '/', do not make the Regexp case insensitive, just translate it to a Regexp with Regexp.new.

Examples:

'/Hello/'.to_regexp #=> /Hello/i
'/Hello/I'.to_regexp #=> /Hello/
'Hello'.to_regexp #=> /Hello/

Returns:

  • (Regexp)

#wrap(width = 70, hang = 0) ⇒ String Originally defined in module FatCore::String

Return a string wrapped to width characters with lines following the first indented by hang characters.

Returns: