Class: Language
- Inherits:
-
Object
show all
- Defined in:
- lib/language/class.rb,
lib/language/codes.rb,
lib/language/mixin.rb,
lib/language/words.rb,
lib/language/censor.rb,
lib/language/matcher.rb
Defined Under Namespace
Modules: Codes, Mixin
Classes: Censor, Matcher
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(subject) ⇒ Language
Returns a new instance of Language.
35
36
37
|
# File 'lib/language/class.rb', line 35
def initialize(subject)
@self = subject
end
|
Class Method Details
.abbreviation ⇒ Object
4
5
6
|
# File 'lib/language/class.rb', line 4
def self.abbreviation
'lang'
end
|
.current ⇒ Object
19
20
21
|
# File 'lib/language/class.rb', line 19
def self.current
@current || default
end
|
.current=(lang) ⇒ Object
24
25
26
|
# File 'lib/language/class.rb', line 24
def self.current=(lang)
@current = lang
end
|
.default ⇒ Object
9
10
11
|
# File 'lib/language/class.rb', line 9
def self.default
@default || abbreviation
end
|
.default=(lang) ⇒ Object
14
15
16
|
# File 'lib/language/class.rb', line 14
def self.default=(lang)
@default = lang
end
|
.instance(string) ⇒ Object
29
30
31
32
|
# File 'lib/language/class.rb', line 29
def self.instance(string)
@cache ||= {}
@cache[string.object_id] = new(string)
end
|
.paragraphs(string, &yld) ⇒ Object
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
# 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) ⇒ Object
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# 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) ⇒ 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
77
78
79
80
81
|
# 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) ⇒ Object
If block given, iterate through each word.
"a string".each_word { |word, range| ... }
Returns an array of words.
"abc 123".words
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
# 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
|
Instance Method Details
#each_paragraph(&blk) ⇒ Object
137
138
139
|
# File 'lib/language/words.rb', line 137
def each_paragraph(&blk)
paragraphs(&blk)
end
|
#each_sentence(&blk) ⇒ Object
128
129
130
|
# File 'lib/language/words.rb', line 128
def each_sentence(&blk)
sentences(&blk)
end
|
#each_word(&blk) ⇒ Object
119
120
121
|
# File 'lib/language/words.rb', line 119
def each_word(&blk)
words(&blk)
end
|
#paragrpahs(&yld) ⇒ Object
132
133
134
|
# File 'lib/language/words.rb', line 132
def paragrpahs(&yld)
self.class.paragraphs(@self, &blk)
end
|
#sentences(&yld) ⇒ Object
123
124
125
|
# File 'lib/language/words.rb', line 123
def sentences(&yld)
self.class.sentences(@self, &blk)
end
|
#word_wrap(col_width = 79) ⇒ Object
142
143
144
|
# 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) ⇒ Object
As with #word_wrap, but modifies the string in place.
147
148
149
|
# File 'lib/language/words.rb', line 147
def word_wrap!(col_width=79)
@self.replace(word_wrap(col_width=79))
end
|
#words(&blk) ⇒ Object
# TODO: This is alternateive from glue: worth providing?
114
115
116
|
# File 'lib/language/words.rb', line 114
def words(&blk)
self.class.words(@self, &blk)
end
|