Class String
In: lib/facet/string/word_filter!.rb
lib/facet/string/dresner.rb
lib/facet/string/blank?.rb
lib/facet/string/unix_crypt.rb
lib/facet/string/center_lines.rb
lib/facet/string/camelcase.rb
lib/facet/string/clump.rb
lib/facet/string/line_to_char_map.rb
lib/facet/string/margin.rb
lib/facet/string/word_wrap.rb
lib/facet/string/unpack.rb
lib/facet/string/rand_letter.rb
lib/facet/string/to_proc.rb
lib/facet/string/first&last.rb
lib/facet/string/chars.rb
lib/facet/string/inflect.rb
lib/facet/string/fuzzy_match.rb
lib/facet/string/succ.rb
lib/facet/string/cmp.rb
lib/facet/string/at_rand.rb
lib/facet/string/range.rb
lib/facet/string/to_b.rb
lib/facet/string/case?.rb
lib/facet/string/to_re.rb
lib/facet/string/line_wrap.rb
lib/facet/string/format.rb
lib/facet/string/to_const.rb
lib/facet/string/tab.rb
lib/facet/string/braket.rb
lib/facet/string/shuffle.rb
lib/facet/string/each_word.rb
lib/facet/string/index_all.rb
lib/facet/string/mscan.rb
lib/facet/string/shatter.rb
lib/facet/string/natcmp.rb
Parent: Object

Methods

Classes and Modules

Class String::Format
Class String::Inflect

Constants

BRA_KET = { '['=>']', '('=>')', '{'=>'}', '<'=>'>' }

External Aliases

unpack -> unpack_from_orgin
succ -> succ_once

Public Class methods

Generate a random letter.

  require 'facet/string/rand_letter'

  String.rand_letter  #=> "q"
  String.rand_letter  #=> "r"
  String.rand_letter  #=> "a"

Public Instance methods

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"! ;)

Inplace version of braket.

camelCase(first=false, on='_\s')

Alias for camelcase

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 an array of characters.

  require 'facet/string/chars'

  "abc".chars  #=> ["a","b","c"]

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"

Inplace version of dresner method.

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"

Returns first n characters.

  require 'facet/string/first&last'

  "Hello World".first(3)  #=> "Hel"

Returns first n characters.

  require 'facet/string/first&last'

  "Hello World".first(3)  #=> "Hel"

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:

  • Converts each string into a "graph like" object, with edges
      "alexsander" - > [ alexsander, alexsand, alexsan ... lexsand ... san ... an, etc ]
      "aleksander" - > [ aleksander, aleksand ... etc. ]
    
  • Perform match, then remove any subsets from this matched set (i.e. a hit on "san" is a subset of a hit on "sander")
      Above example, once reduced -> [ ale, sander ]
    
  • See’s how many of the matches remain, and calculates a score based on how many matches, their length, and compare to the length of the larger of the two words.

Indent left or right by n spaces. (This used to be called tab and aliased as indent.)

  require 'facet/string/tab'

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 last n characters.

  require 'facet/string/first&last'

  "Hello World".last(3)  #=> "rld"

Returns last n characters.

  require 'facet/string/first&last'

  "Hello World".last(3)  #=> "rld"

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
lowercase?()

Alias for downcase?

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

Return a random byte of self.

  require 'facet/string/at_rand'

  "Ruby rules".rand_byte  #=> 121

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"

Return a random string index.

  require 'facet/string/at_rand'

  "Ruby rules".rand_index  #=> 3

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"

Aligns each line n spaces this used to be taballto.

  require 'facet/string/tab'

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.

Evaluates a String as a Proc.

  require 'facet/string/to_proc'

  xyp = "|x,y| x + y".to_proc
  xyp.class      #=> Proc
  xyp.call(1,2)  #=> 3

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  #=> /\[/

Same as margin, but the parameters are in reverse order.

Common Unix cryptography method.

Common Unix cryptography in-place method.

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
uppercase?()

Alias for upcase?

whitespace?()

Alias for blank?

In place word filter.

  require 'facet/string/word_filter!'

  "a string".word_filter! { |word| ... }

Word wrap a string not exceeding max width.

  require 'facet/string/word_wrap'

  puts "this is a test".word_wrap(4)

produces

  this
  is a
  test

Like word_wrap, but modifies the string in place.

[Validate]