Module: English::String
- Included in:
- String
- Defined in:
- lib/gems/english-0.3.1/lib/english/string.rb
Instance Method Summary collapse
-
#brief(range = 76, ellipsis = "...") ⇒ Object
Returns short abstract of long strings; not exceeding
range
characters. -
#each_word_with_range(&yld) ⇒ Object
Iterate through each word of a string.
-
#fold(ignore_indented = false) ⇒ Object
Returns a new string with all new lines removed from adjacent lines of text.
-
#word_filter(&blk) ⇒ Object
Filters out words from a string based on block test.
-
#word_filter! ⇒ Object
In place version of #word_filter.
-
#word_wrap(col_width = 80) ⇒ Object
Word wrap a string not exceeding max width.
-
#word_wrap!(col_width = 80) ⇒ Object
As with #word_wrap, but modifies the string in place.
-
#words ⇒ Object
Returns an array of characters.
Instance Method Details
#brief(range = 76, ellipsis = "...") ⇒ Object
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
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/gems/english-0.3.1/lib/english/string.rb', line 181 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_with_range(&yld) ⇒ Object
Iterate through each word of a string.
"a string".each_word { |word, range| ... }
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/gems/english-0.3.1/lib/english/string.rb', line 17 def each_word_with_range(&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 |
#fold(ignore_indented = false) ⇒ Object
Returns a new string with all new lines removed from adjacent lines of text.
s = "This is\na test.\n\nIt clumps\nlines of text."
s.fold
produces
"This is a test.\n\nIt clumps lines of text. "
One arguable flaw with this, that might need a fix: if the given string ends in a newline, it is replaced with a single space.
CREDIT: Trans
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/gems/english-0.3.1/lib/english/string.rb', line 153 def fold(ignore_indented=false) ns = '' i = 0 br = self.scan(/(\n\s*\n|\Z)/m) do |m| b = $~.begin(1) e = $~.end(1) nl = $& tx = slice(i...b) if ignore_indented and slice(i...b) =~ /^[ ]+/ ns << tx else ns << tx.gsub(/[ ]*\n+/,' ') end ns << nl i = e end ns end |
#word_filter(&blk) ⇒ Object
Filters out words from a string based on block test.
"a string".word_filter { |word| word =~ /^a/ } #=> "string"
CREDIT: George Moschovitis
41 42 43 44 |
# File 'lib/gems/english-0.3.1/lib/english/string.rb', line 41 def word_filter( &blk ) s = self.dup s.word_filter!( &blk ) end |
#word_filter! ⇒ Object
In place version of #word_filter.
"a string".word_filter { |word| ... }
CREDIT: George Moschovitis
52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/gems/english-0.3.1/lib/english/string.rb', line 52 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) ⇒ Object
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
108 109 110 |
# File 'lib/gems/english-0.3.1/lib/english/string.rb', line 108 def word_wrap( col_width=80 ) self.dup.word_wrap!( col_width ) end |
#word_wrap!(col_width = 80) ⇒ Object
As with #word_wrap, but modifies the string in place.
CREDIT: Gavin Kistner
CREDIT: Dayne Broderson
117 118 119 120 121 |
# File 'lib/gems/english-0.3.1/lib/english/string.rb', line 117 def word_wrap!( col_width=80 ) self.gsub!( /(\S{#{col_width}})(?=\S)/, '\1 ' ) self.gsub!( /(.{1,#{col_width}})(?:\s+|$)/, "\\1\n" ) self end |
#words ⇒ Object
Returns an array of characters.
"abc 123".words #=> ["abc","123"]
9 10 11 |
# File 'lib/gems/english-0.3.1/lib/english/string.rb', line 9 def words self.split(/\s+/) end |