# File lib/language/words.rb, line 137 def each_paragraph(&blk) paragraphs(&blk) end
# File lib/language/words.rb, line 128 def each_sentence(&blk) sentences(&blk) end
# File lib/language/words.rb, line 119 def each_word(&blk) words(&blk) end
# File lib/language/words.rb, line 132 def paragrpahs(&yld) self.class.paragraphs(@self, &blk) end
# File lib/language/words.rb, line 123 def sentences(&yld) self.class.sentences(@self, &blk) end
# File lib/language/words.rb, line 142 def word_wrap(col_width=79) self.class.word_wrap(@self, col_width) end
As with word_wrap, but modifies the string in place.
# File lib/language/words.rb, line 147 def word_wrap!(col_width=79) @self.replace(word_wrap(col_width=79)) end
TODO: This is alternateive from glue: worth providing?
Enforces a maximum width of a string inside an html container. If the string exceeds this maximum width the string gets wraped.
Not really useful, better use the CSS overflow: hidden functionality.
the string to be wrapped the enforced width the separator used for wrapping
the wrapped string
text = "1111111111111111111111111111111111111111111" text = wrap(text, 10, " ") p text # => "1111111111 1111111111 1111111111"
See the test cases to better understand the behaviour!
def wrap(width = 20, separator = " ") re = /([^#{separator}]{1,#{width}})/ scan(re).join(separator) end
# File lib/language/words.rb, line 114 def words(&blk) self.class.words(@self, &blk) end
# File lib/language/class.rb, line 4 def self.abbreviation 'lang' end
# File lib/language/class.rb, line 19 def self.current @current || default end
# File lib/language/class.rb, line 24 def self.current=(lang) @current = lang end
# File lib/language/class.rb, line 9 def self.default @default || abbreviation end
# File lib/language/class.rb, line 14 def self.default=(lang) @default = lang end
# File lib/language/class.rb, line 29 def self.instance(string) @cache ||= {} @cache[string.object_id] = new(string) end
# File lib/language/class.rb, line 35 def initialize(subject) @self = subject end
# File lib/language/words.rb, line 49 def self.paragraphs(string, &yld) if block_given? string.scan(/(.*?\n\s{2,})/).each do |paragraph| range = $~.begin(0)...$~.end(0) if yld.arity == 1 yld.call(paragraph) else yld.call(paragraph, range) end end else string.scan(/(.*?\n\s{2,})/) end end
# File lib/language/words.rb, line 33 def self.sentences(string, &yld) if block_given? string.scan(/(.*?\.\ )/).each do |sentence| range = $~.begin(0)...$~.end(0) if yld.arity == 1 yld.call(sentence) else yld.call(sentence, range) end end else string.scan(/(.*?\.\ )/) end 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
# File lib/language/words.rb, line 77 def self.word_wrap(string, col_width=79) string = string.gsub( /(\S{#{col_width}})(?=\S)/, '\1 ' ) string = string.gsub( /(.{1,#{col_width}})(?:\s+|$)/, "\\1\n" ) string end
If block given, iterate through each word.
"a string".each_word { |word, range| ... }
Returns an array of words.
"abc 123".words #=> ["abc","123"]
# File lib/language/words.rb, line 17 def self.words(string, &yld) if block_given? string.scan(/([-'\w]+)/).each do |word| range = $~.begin(0)...$~.end(0) if yld.arity == 1 yld.call(word) else yld.call(word, range) end end else string.scan(/([-'\w]+)/).flatten end end