Methods
Public Instance methods
brief(range=76, ellipsis="...")

Returns short abstract of long strings; not exceeding range characters. If range is an integer then the minimum is 20% of the maximum. The string is chopped at the nearest word if possible, and appended by ellipsis, which defaults to ’…’.

  CREDIT: George Moschovitis
  CREDIT: Trans
# File lib/core/facets/string/words.rb, line 148
    def brief(range=76, ellipsis="...")
      if Range===range
        min = range.first
        max = range.last
      else
        max = range
        min = max - (max/5).to_i
        range = min..max
      end

      if size > max
        cut_at = rindex(/\b/, max) || max
        cut_at = max if cut_at < min
        xstring = slice(0, cut_at)
        xstring.chomp(" ") + ellipsis
      else
        self
      end
    end
brief(range=76, ellipsis="...")

Returns short abstract of long strings; not exceeding range characters. If range is an integer then the minimum is 20% of the maximum. The string is chopped at the nearest word if possible, and appended by ellipsis, which defaults to ’…’.

  CREDIT: George Moschovitis
  CREDIT: Trans
# File lib/core/facets/string/words.rb, line 148
    def brief(range=76, ellipsis="...")
      if Range===range
        min = range.first
        max = range.last
      else
        max = range
        min = max - (max/5).to_i
        range = min..max
      end

      if size > max
        cut_at = rindex(/\b/, max) || max
        cut_at = max if cut_at < min
        xstring = slice(0, cut_at)
        xstring.chomp(" ") + ellipsis
      else
        self
      end
    end
each_word( &yld )

Iterate through each word of a string.

  "a string".each_word { |word, range| ... }
# File lib/core/facets/string/words.rb, line 19
    def each_word( &yld )
      rest_of_string = self
      wordfind = /([-'\w]+)/
      arity = yld.arity
      offset = 0
      while wmatch = wordfind.match(rest_of_string)
        word = wmatch[0]
        range = offset+wmatch.begin(0) ... offset+wmatch.end(0)
        rest_of_string = wmatch.post_match
        if arity == 1
          yld.call(word)
        else
          yld.call(word, range)
        end
        offset = self.length - rest_of_string.length
      end
    end
each_word( &yld )

Iterate through each word of a string.

  "a string".each_word { |word, range| ... }
# File lib/core/facets/string/words.rb, line 19
    def each_word( &yld )
      rest_of_string = self
      wordfind = /([-'\w]+)/
      arity = yld.arity
      offset = 0
      while wmatch = wordfind.match(rest_of_string)
        word = wmatch[0]
        range = offset+wmatch.begin(0) ... offset+wmatch.end(0)
        rest_of_string = wmatch.post_match
        if arity == 1
          yld.call(word)
        else
          yld.call(word, range)
        end
        offset = self.length - rest_of_string.length
      end
    end
word_filter( &blk )

Filters out words from a string based on block test.

  "a string".word_filter { |word| word =~ /^a/ }  #=> "string"

  CREDIT: George Moschovitis
# File lib/core/facets/string/words.rb, line 43
    def word_filter( &blk )
      s = self.dup
      s.word_filter!( &blk )
    end
word_filter( &blk )

Filters out words from a string based on block test.

  "a string".word_filter { |word| word =~ /^a/ }  #=> "string"

  CREDIT: George Moschovitis
# File lib/core/facets/string/words.rb, line 43
    def word_filter( &blk )
      s = self.dup
      s.word_filter!( &blk )
    end
word_filter!( {|| ...}

In place version of word_filter.

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

  CREDIT: George Moschovitis
# File lib/core/facets/string/words.rb, line 54
    def word_filter! #:yield:
      rest_of_string = self
      wordfind = /(\w+)/
      offset = 0
      while wmatch = wordfind.match(rest_of_string)
        word = wmatch[0]
        range = offset+wmatch.begin(0) ... offset+wmatch.end(0)
        rest_of_string = wmatch.post_match
        self[range] = yield( word ).to_s
        offset = self.length - rest_of_string.length
      end
      self
    end
word_filter!( {|| ...}

In place version of word_filter.

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

  CREDIT: George Moschovitis
# File lib/core/facets/string/words.rb, line 54
    def word_filter! #:yield:
      rest_of_string = self
      wordfind = /(\w+)/
      offset = 0
      while wmatch = wordfind.match(rest_of_string)
        word = wmatch[0]
        range = offset+wmatch.begin(0) ... offset+wmatch.end(0)
        rest_of_string = wmatch.post_match
        self[range] = yield( word ).to_s
        offset = self.length - rest_of_string.length
      end
      self
    end
word_wrap( col_width=80 )

Word wrap a string not exceeding max width.

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

produces

  this
  is a
  test

  CREDIT: Gavin Kistner
  CREDIT: Dayne Broderson
# File lib/core/facets/string/words.rb, line 110
    def word_wrap( col_width=80 )
      self.dup.word_wrap!( col_width )
    end
word_wrap( col_width=80 )

Word wrap a string not exceeding max width.

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

produces

  this
  is a
  test

  CREDIT: Gavin Kistner
  CREDIT: Dayne Broderson
# File lib/core/facets/string/words.rb, line 110
    def word_wrap( col_width=80 )
      self.dup.word_wrap!( col_width )
    end
word_wrap!( col_width=80 )

As with word_wrap, but modifies the string in place.

  CREDIT: Gavin Kistner
  CREDIT: Dayne Broderson
# File lib/core/facets/string/words.rb, line 119
    def word_wrap!( col_width=80 )
      self.gsub!( /(\S{#{col_width}})(?=\S)/, '\1 ' )
      self.gsub!( /(.{1,#{col_width}})(?:\s+|$)/, "\\1\n" )
      self
    end
word_wrap!( col_width=80 )

As with word_wrap, but modifies the string in place.

  CREDIT: Gavin Kistner
  CREDIT: Dayne Broderson
# File lib/core/facets/string/words.rb, line 119
    def word_wrap!( col_width=80 )
      self.gsub!( /(\S{#{col_width}})(?=\S)/, '\1 ' )
      self.gsub!( /(.{1,#{col_width}})(?:\s+|$)/, "\\1\n" )
      self
    end
words()

Returns an array of characters.

  "abc 123".words  #=> ["abc","123"]
# File lib/core/facets/string/words.rb, line 11
    def words
      self.split(/\s+/)
    end
words()

Returns an array of characters.

  "abc 123".words  #=> ["abc","123"]
# File lib/core/facets/string/words.rb, line 11
    def words
      self.split(/\s+/)
    end