BRA_KET | = | { '['=>']', '('=>')', '{'=>'}', '<'=>'>' } |
unpack | -> | unpack_from_orgin |
succ | -> | succ_once |
Generate a random letter.
require 'facet/string/rand_letter' String.rand_letter #=> "q" String.rand_letter #=> "r" String.rand_letter #=> "a"
Return a random separation of the string. Default separator is $/.
require 'facet/string/at_rand' "Ruby rules".at_rand(1, ' ') #=> ["Ruby"]
Return a random separation while removing it from the string. Default separator is $/.
require 'facet/string/at_rand' "Ruby rules".at_rand(1, ' ') #=> ["Ruby"]
Is this string just whitespace?
require 'facet/string/blank?' "abc".blank? #=> false " ".blank? #=> true
Return a new string embraced by given brakets. If only one bracket char is given it will be placed on either side.
require 'facet/string/braket' "wrap me".braket('{') #=> "{wrap me}" "wrap me".braket('--','!') #=> "--wrap me!"
And yes, I know, it’s spelled "wrong"! ;)
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.
require 'facet/string/camelcase' "this_is a test".camelCase #=> "thisIsATest" "this_is a test".camelCase(true) #=> "ThisIsATest" "this_is a test".camelCase(true, ' ') #=> "This_isATest"
Is the string capitalized?
require 'facet/string/case?' "THIS".capitalized? #=> true "This".capitalized? #=> true "this".capitalized? #=> false
Centers each newline of a string. (This is probably how center itself should work.)
require 'facet/string/center_lines' s = <<-EOS This is a test and so on EOS puts s.center_lines(14)
produces
This is a test and so on
Returns a new string with all new lines removed from adjacent lines of text.
require 'facet/string/clump' s = "This is\na test.\n\nIt clumps\nlines of text." s.clump
produces
"This is a test.\n\nIt clumps lines of text. "
Compare method that takes length into account. Unlike #<=>, this is compatible with succ.
require 'facet/string/cmp' "abc".cmp("abc") #=> 0 "abcd".cmp("abc") #=> 1 "abc".cmp("abcd") #=> -1 "xyz".cmp("abc") #=> 1
Is the string downcase/lowercase?
require 'facet/string/case?' "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:
Srblamce the iennr cchrteaars of wodrs lvenaig the txet stlil rbeaadle (rreceash at Cbamigdre Uverintisy, cdoe by KrneruestDr?)
Usage:
require 'facet/string/dresner' "this".dresner #=> "tihs"
Iterate through each word of a string.
require 'facet/string/each_word' "a string".each_word { |word, range| ... }
Returns first n separations. The default separator is $/. If a zero-length record separator is supplied, the string is split on /\n+/. If the record separator is set to nil, then the string is split on /\s+/.
require 'facet/string/first&last' "Hello World".first(3) #=> "Hel"
Prepends a string to front of the string.
require 'facet/string/first&last' "Hello World".first = "Hello," #=> "Hello, Hello World"
Replaces the first seperation with a new string. If a zero-length record separator is supplied, the string is split on /\n+/. If the record separator is set to nil, then the string is split on characters.
require 'facet/string/first&last' $/ = nil "Hello World".first = "Hello," #=> "Hello, Hello World"
The format mehtod is a special toolbox method, or methodbox. Within it are a collection of finer methods.
require 'facet/string/format' "dogthispony".format.camelcase #=> "DogThisPony"
A fuzzy matching mechanism. Returns a score from 0-1, based on the number of shared edges. (matches of a sequence of characters of length 2 or more)
"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 ]
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.
require 'facet/string/index_all' "abcabcabc".index_all('a') #=> [0,3,6]
Returns last n separations. If a zero-length record separator is supplied, the string is split on /\n+/. If the record separator is set to nil, then the string is split on /\s+/.
require 'facet/string/first&last' "Hello World".last(1,nil) #=> "World"
Appends a string to end of the string.
require 'facet/string/first&last' "Hello World".last = ". Bye." #=> "Hello World. Bye."
Replaces the last separation with a new string. If a zero-length record separator is supplied, the string is split on /\n+/. If the record separator is set to nil, then the string is split on characters. #
require 'facet/string/first&last' $/ = '' "Hello World".last = ", Bye." #=> "Hello World, Bye."
Returns an array of ranges mapping the characters per line.
require 'facet/string/line_to_char_map' "this\nis\na\ntest".line_to_char_map #=> [0..4, 5..7, 8..9, 10..13]
Line wrap at width.
require 'facet/string/line_wrap' puts "Here and there".line_wrap(5)
produces
Here and t here
Provides a margin controlled string.
require 'facet/string/margin' x = %Q{ | This | is | margin controlled! }.margin
Like scan but returns MatchData ($~) rather then matched string ($&).
require 'facet/string/mscan'
‘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.
require 'facet/string/natcmp' "Hello.10".natcmp("Hello.1") #=> -1
Destructive pick_byte. Delete a random byte of self and return it.
require 'facet/string/at_rand' s = "Ruby rules" s.pick_byte! #=> 121 s #=> "Rub rules"
Return a single-character string of a random character in self.
require 'facet/string/at_rand' "Ruby rules".rand_char #=> "y"
Destructive pick_char. Delete a random character of the string and return it as a single-character string.
require 'facet/string/at_rand' s = "Ruby rules" s.pick_char! #=> "y" s #=> "Rub rules"
Like index but returns a Range.
require 'facet/string/range' "This is a test!".rand('test') #=> 10..13
Like index_all but returns an array of Ranges.
require 'facet/string/range' "abc123abc123".range_all('abc') #=> [0..2, 6..8]
Breaks a string up into an array based on a regular expression. Similar to scan, but includes the matches.
require 'facet/string/shatter' s = "<p>This<b>is</b>a test.</p>" s.shatter( // )
produces
["", "<p>", "This", "<b>", "is", "</b>", "a test.", "</p>"]
Return the string with characters arranged in random order.
require 'facet/string/pick' "Ruby rules".shuffle_chars #=> "e lybRsuur"
Destructive shuffle_chars. Arrange the characters of the string in a new random order.
require 'facet/string/pick' s = "Ruby rules".shuffle_chars s.shuffle_chars! s #=> "e lybRsuur"
Return the string with characters arranged in random order.
require 'facet/string/pick' "Ruby rules".shuffle_chars #=> "e lybRsuur"
Destructive shuffle_chars. Arrange the characters of the string in a new random order.
require 'facet/string/pick' s = "Ruby rules".shuffle_chars s.shuffle_chars! s #=> "e lybRsuur"
Allows succ to take n step increments.
require 'facet/string/succ' "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.
require 'facet/string/tab'
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:
require 'facet/string/to_b' "true".to_b #=> true "yes".to_b #=> true "no".to_b #=> false "123".to_b #=> false
Get a constant by a given string name.
require 'facet/string/to_const' "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.
Turns a string into a regular expression. By default it will escape all characters. Use false argument to turn off escaping.
require 'facet/string/to_re' "[".to_re #=> /\[/
Upack with offset. Extends unpack to allow a string to be unpacked starting at an offset position within it.
require 'facet/string/unpack'
Is the string upcase/uppercase?
require 'facet/string/case?' "THIS".upcase? #=> true "This".upcase? #=> false "this".upcase? #=> false