slice | -> | at |
Alias for slice which increases polymorphism with Array. | ||
downcase? | -> | lowercase? |
Alias for downcase? method. | ||
unpack | -> | unpack_from_orgin |
basename | -> | demodulize |
succ | -> | succ_once |
Returns a randomly generated string. One possible use is password initialization. Takes a max legnth of characters (default 8) and an optional valid char Regexp (default /\w\d/).
Centers each line of a string.
s = <<-EOS This is a test and so on EOS puts s.align_center(14)
produces
This is a test and so on
Align a string to the center. The defualt alignment seperation is a new line ("/n") This can be changed as can be the padding string which defaults to a single space (’ ’).
Align a string to the left. The defualt alignment seperation is a new line ("/n") This can be changes as can be the padding string which defaults to a single space (’ ’).
Align a string to the right. The defualt alignment seperation is a new line ("/n") This can be changes as can be the padding string which defaults to a single space (’ ’).
Return a random separation of the string. Default separation is by charaacter.
"Ruby rules".at_rand(' ') #=> ["Ruby"]
Return a random separation while removing it from the string. Default separation is by character.
s = "Ruby rules" s = at_rand!(' ') #=> "Ruby" s #=> "rules"
Return a new string embraced by given brakets. If only one bracket char is given it will be placed on either side.
"wrap me".bracket('{') #=> "{wrap me}" "wrap me".bracket('--','!') #=> "--wrap me!"
Converts a string to camelcase. By default capitalization occurs on whitespace and underscores. By setting the first parameter to true the first character can also be captizlized. The second parameter can be assigned a valid Regualr Expression characeter set to determine which characters to match for capitalizing subsequent parts of the string.
"this_is a test".camelcase #=> "thisIsATest" "this_is a test".camelcase(true) #=> "ThisIsATest" "this_is a test".camelcase(true, ' ') #=> "This_isATest"
Capitalize all words (or other patterned divisions) of a string.
"this is a test".capitalize_all #=> "This Is A Test"
Return true if the string is capitalized, otherwise false.
"THIS".capitalized? #=> true "This".capitalized? #=> true "this".capitalized? #=> false
Compare method that takes length into account. Unlike #<=>, this is compatible with succ.
"abc".cmp("abc") #=> 0 "abcd".cmp("abc") #=> 1 "abc".cmp("abcd") #=> -1 "xyz".cmp("abc") #=> 1
Breaks a string up into an array based on a regular expression. Similar to scan, but includes the matches.
s = "<p>This<b>is</b>a test.</p>" s.divide( /\<.*?\>/ )
produces
["<p>This", "<b>is", "</b>a test.", "</p>"]
Return true if the string is lowercase (downcase), otherwise false.
"THIS".downcase? #=> false "This".downcase? #=> false "this".downcase? #=> true
Scramble the inner characters of words leaving the text still readable (research at Cambridge University, code by KurtDresner).
For example, the above text may result in:
Srblamce the iennr cchrteaars of wodrs lvenaig the txet stlil rbeaadle (rreceash at Cbamigdre Uverintisy, cdoe by KrneruestDr?)
Returns the first separation of a string. Default seperation is by character.
"Hello World".first #=> "H" "Hello World".first(' ') #=> "Hello"
Removes the first separation from a string. Defualt separation is by characters.
a = "Hello World" a.first! #=> "H" a #=> "ello World" a = "Hello World" a.first!(' ') #=> "Hello" a #=> "World"
Like index but returns an array of all index locations. The reuse flag allows the trailing portion of a match to be reused for subsquent matches.
"abcabcabc".index_all('a') #=> [0,3,6]
Returns the last separation of a string. Default separation is by character.
"Hello World".last(' ') #=> "World"
Removes the last separation from a string. Default seperation is by characeter.
a = "Hello World" a.last! #=> "d" a #=> "Hello Worl" a = "Hello World" a.last!(' ') #=> "World" a #=> "Hello"
Converts a string into a valid ruby method name This method is geared toward code reflection.
"MyModule::MyClass".methodize #=> "my_module__my_class"
See also String#modulize, String#pathize
Converts a string into a valid ruby class or module name This method is geared toward code reflection.
"my_module__my_path".modulize #=> "MyModule::MyPath"
See also String#methodize, String#pathize
TODO
‘Natural order’ comparison of two strings, e.g.
"my_prog_v1.1.0" < "my_prog_v1.2.0" < "my_prog_v1.10.0"
which does not follow alphabetically. A secondary parameter, if set to true, makes the comparison case insensitive.
"Hello.10".natcmp("Hello.1") #=> -1
Retrns n characters of the string. If n is positive the characters are from the beginning of the string. If n is negative from the end of the string.
Alternatively a replacement string can be given, which will replace the n characters.
Converts a string into a unix path. This method is geared toward code reflection.
See : String#modulize, String#methodize
"MyModule::MyClass".pathize #=> my_module/my_class "my_module__my_class".pathize #=> my_module/my_class
TODO :
Convert an English word from singular to plurel.
"boy".plural #=> boys "tomato".plural #=> tomatoes
Return a new string embraced by given quotes. If no quotes are specified, then assumes single quotes.
"quote me".quote #=> "'quote me'" "quote me".quote(2) #=> "\"quote me\""
Like index_all but returns an array of Ranges.
"abc123abc123".range_all('abc') #=> [0..2, 6..8]
Returns an array of ranges mapping the characters per line.
"this\nis\na\ntest".range_of_line #=> [0..4, 5..7, 8..9, 10..13]
Apply a set of rules (regular expression matches) to the string.
The rules must be applied in order! So we cannot use a hash because the ordering is not guaranteed! we use an array instead.
The array containing rule-pairs (match, write).
The rewritten string.
Breaks a string up into an array based on a regular expression. Similar to scan, but includes the matches.
s = "<p>This<b>is</b>a test.</p>" s.shatter( /\<.*?\>/ )
produces
["<p>", "This", "<b>", "is", "</b>", "a test.", "</p>"]
Return the string with seperated sections arranged in a random order. The default seperation is by character.
"Ruby rules".shuffle #=> "e lybRsuur"
A fuzzy matching mechanism. Returns a score from 0-1, based on the number of shared edges. To be effective, the strings must be of length 2 or greater.
"Alexsander".fuzzy_match( "Aleksander" ) #=> 0.9
The way it works:
"alexsander" -> [ alexsander, alexsand, alexsan ... lexsand ... san ... an, etc ] "aleksander" -> [ aleksander, aleksand ... etc. ]
Above example, once reduced -> [ ale, sander ]
Convert an English word from plurel to singular.
"boys".singular #=> boy "tomatoes".singular #=> tomato
Implementation of the soundex algorithm as described by Knuth in volume 3 of The Art of Computer Programming. Returns nil if the value couldn‘t be calculated b/c of empty-string or invalid character.
"Ruby".soundex #=> "R100"
Allows succ to take n step increments.
"abc".succ #=> "abd" "abc".succ(4) #=> "abg" "abc".succ(24) #=> "aca"
Preserves relative tabbing. The first non-empty line ends up with n spaces before nonspace.
Essentially makes to_a an alias for split, with the excpetion that if no divider is given then the array is split on charaters, and NOT on the global input record divider ($/).
WARNING There is a slight chance of incompatability with other libraries which depend on spliting with $/ (although doing so is a very bad idea!).
Interpret common affirmative string meanings as true, otherwise false. Balnk sapce and case are ignored. The following strings that will return true:
<tt>true</tt>,<tt>yes</tt>,<tt>on</tt>,<tt>t</tt>,<tt>1</tt>,<tt>y</tt>,<tt>==</tt>
Examples:
"true".to_b #=> true "yes".to_b #=> true "no".to_b #=> false "123".to_b #=> false
Get a constant by a given string name.
"Class".to_const #=> Class
Note this method is not as verstile as it should be, since it can not access contants relative to the current execution context. But without a binding_of_caller that does not seem possible.
This method creates a raw object, that can be nested into other data structures and will be unparsed as a raw string.
Turns a string into a regular expression. By default it will escape all characters. Use false argument to turn off escaping.
"[".to_re #=> /\[/
Turns a string into a regular expression. Unlike to_re this will not escape characters.
"a?".to_rx #=> /a?/
Translates a string in the form on a set of numerical and/or alphanumerical characters separated by non-word characters (eg \W+) into a Tuple. The values of the tuple will be converted to integers if they are purely numerical.
'1.2.3a'.to_t #=> [1,2,"3a"]
It you would like to control the interpretation of each value as it is added to the tuple you can supply a block.
'1.2.3a'.to_t { |v| v.upcase } #=> ["1","2","3A"]
This method calls Tuple.cast_from_string.
Return a new string embraced by given brakets. If only one bracket char is given it will be placed on either side.
"{unwrap me}".debracket('{') #=> "unwrap me" "--unwrap me!".debracket('--','!') #=> "unwrap me!"
Upack with offset. Extends unpack to allow a string to be unpacked starting at an offset position within it.
Is the string upcase/uppercase?
"THIS".upcase? #=> true "This".upcase? #=> false "this".upcase? #=> false
Filters out words from a string based on block test.
"a string".word_filter { |word| word =~ /^a/ } #=> "string"