class Language

Public Instance Methods

each_paragraph(&blk) click to toggle source
# File lib/language/words.rb, line 137
def each_paragraph(&blk)
  paragraphs(&blk)
end
each_sentence(&blk) click to toggle source
# File lib/language/words.rb, line 128
def each_sentence(&blk)
  sentences(&blk)
end
each_word(&blk) click to toggle source
# File lib/language/words.rb, line 119
def each_word(&blk)
  words(&blk)
end
paragrpahs(&yld) click to toggle source
# File lib/language/words.rb, line 132
def paragrpahs(&yld)
  self.class.paragraphs(@self, &blk)
end
sentences(&yld) click to toggle source
# File lib/language/words.rb, line 123
def sentences(&yld)
  self.class.sentences(@self, &blk)
end
word_wrap(col_width=79) click to toggle source
# File lib/language/words.rb, line 142
def word_wrap(col_width=79)
  self.class.word_wrap(@self, col_width)
end
word_wrap!(col_width=79) click to toggle source

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
words(&blk) click to toggle source

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.

Input:

the string to be wrapped the enforced width the separator used for wrapping

Output:

the wrapped string

Example:

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

Public Class Methods

abbreviation() click to toggle source
# File lib/language/class.rb, line 4
def self.abbreviation
  'lang'
end
current() click to toggle source
# File lib/language/class.rb, line 19
def self.current
  @current || default
end
current=(lang) click to toggle source
# File lib/language/class.rb, line 24
def self.current=(lang)
  @current = lang
end
default() click to toggle source
# File lib/language/class.rb, line 9
def self.default
  @default || abbreviation
end
default=(lang) click to toggle source
# File lib/language/class.rb, line 14
def self.default=(lang)
  @default = lang
end
instance(string) click to toggle source
# File lib/language/class.rb, line 29
def self.instance(string)
  @cache ||= {}
  @cache[string.object_id] = new(string)
end
new(subject) click to toggle source
# File lib/language/class.rb, line 35
def initialize(subject)
  @self = subject
end
paragraphs(string, &yld) click to toggle source
# 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
sentences(string, &yld) click to toggle source
# 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(string, col_width=79) click to toggle source

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
words(string, &yld) click to toggle source

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