- brief
- brief
- each_word
- each_word
- word_filter
- word_filter
- word_filter!
- word_filter!
- word_wrap
- word_wrap
- word_wrap!
- word_wrap!
- words
- words
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
[ show source ]
# 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
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
[ show source ]
# 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
Iterate through each word of a string.
"a string".each_word { |word, range| ... }
[ show source ]
# 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
Iterate through each word of a string.
"a string".each_word { |word, range| ... }
[ show source ]
# 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
Filters out words from a string based on block test.
"a string".word_filter { |word| word =~ /^a/ } #=> "string" CREDIT: George Moschovitis
[ show source ]
# File lib/core/facets/string/words.rb, line 43 def word_filter( &blk ) s = self.dup s.word_filter!( &blk ) end
Filters out words from a string based on block test.
"a string".word_filter { |word| word =~ /^a/ } #=> "string" CREDIT: George Moschovitis
[ show source ]
# File lib/core/facets/string/words.rb, line 43 def word_filter( &blk ) s = self.dup s.word_filter!( &blk ) end
In place version of word_filter.
"a string".word_filter { |word| ... } CREDIT: George Moschovitis
[ show source ]
# 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
In place version of word_filter.
"a string".word_filter { |word| ... } CREDIT: George Moschovitis
[ show source ]
# 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 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
[ show source ]
# File lib/core/facets/string/words.rb, line 110 def word_wrap( col_width=80 ) self.dup.word_wrap!( col_width ) end
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
[ show source ]
# File lib/core/facets/string/words.rb, line 110 def word_wrap( col_width=80 ) self.dup.word_wrap!( col_width ) end
As with word_wrap, but modifies the string in place.
CREDIT: Gavin Kistner CREDIT: Dayne Broderson
[ show source ]
# 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
As with word_wrap, but modifies the string in place.
CREDIT: Gavin Kistner CREDIT: Dayne Broderson
[ show source ]
# 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
Returns an array of characters.
"abc 123".words #=> ["abc","123"]
[ show source ]
# File lib/core/facets/string/words.rb, line 11 def words self.split(/\s+/) end
Returns an array of characters.
"abc 123".words #=> ["abc","123"]
[ show source ]
# File lib/core/facets/string/words.rb, line 11 def words self.split(/\s+/) end