Class: String
Overview
Copyright Freya Dorn <[email protected]>, 2013 License: GNU GPL 3 <www.gnu.org/copyleft/gpl.html>
Constant Summary collapse
- ANSIColorRegexp =
Regexp.new(/(?: \e \[ \d+ (?: ;\d+)* m )+/x)
- HALFWIDTH_KANA =
"。「」、・ヲァィゥェォャュョッーアイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワン゙゚"- FULLWIDTH_KANA =
"。「」、・ヲァィゥェォャュョッーアイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワン゛゜"
Instance Method Summary collapse
- #color(name) ⇒ Object
- #first(limit = 1) ⇒ Object
- #fullwidth ⇒ Object
- #indent(amount, indent_string = nil, indent_empty_lines = false) ⇒ Object
- #indent!(amount, indent_string = nil, indent_empty_lines = false) ⇒ Object
- #last(limit = 1) ⇒ Object
- #mgsub(replacements = {}) ⇒ Object (also: #gsub_all)
- #mgsub!(replacements = {}) ⇒ Object (also: #gsub_all!)
- #truncate(length, omission: "...") ⇒ Object
- #whackuum(str = /\s+/) ⇒ Object
Instance Method Details
#color(name) ⇒ Object
66 67 68 |
# File 'lib/muflax/string.rb', line 66 def color name HighLine.color(self, name) end |
#first(limit = 1) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/muflax/string.rb', line 42 def first limit=1 if limit == 0 ; "" elsif limit >= size ; dup else ; self[0..(limit - 1)] end end |
#fullwidth ⇒ Object
73 74 75 |
# File 'lib/muflax/string.rb', line 73 def fullwidth tr(" !-~" + HALFWIDTH_KANA, "\u3000" + (0xFF01...0xFF5f).to_a.pack('U*') + FULLWIDTH_KANA) end |
#indent(amount, indent_string = nil, indent_empty_lines = false) ⇒ Object
62 63 64 |
# File 'lib/muflax/string.rb', line 62 def indent amount, indent_string = nil, indent_empty_lines = false dup.tap { |_| _.indent!(amount, indent_string, indent_empty_lines) } end |
#indent!(amount, indent_string = nil, indent_empty_lines = false) ⇒ Object
56 57 58 59 60 |
# File 'lib/muflax/string.rb', line 56 def indent! amount, indent_string = nil, indent_empty_lines = false indent_string = indent_string || self[/^[ \t]/] || " " re = indent_empty_lines ? /^/ : /^(?!$)/ gsub!(re, indent_string * amount) end |
#last(limit = 1) ⇒ Object
49 50 51 52 53 54 |
# File 'lib/muflax/string.rb', line 49 def last limit=1 if limit == 0 ; "" elsif limit >= size ; dup else ; self[(-limit)..-1] end end |
#mgsub(replacements = {}) ⇒ Object Also known as: gsub_all
7 8 9 10 11 12 13 |
# File 'lib/muflax/regex.rb', line 7 def mgsub replacements={} str = self.dup str.mgsub! replacements str end |
#mgsub!(replacements = {}) ⇒ Object Also known as: gsub_all!
16 17 18 19 20 21 22 |
# File 'lib/muflax/regex.rb', line 16 def mgsub! replacements={} replacements.each do |a, b| self.gsub! a, b end self end |
#truncate(length, omission: "...") ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/muflax/string.rb', line 16 def truncate length, omission: "..." return self if self.length <= length ss = StringScanner.new(self) out = ss.scan(ANSIColorRegexp) || "" cut_length = length - omission.str_length used = 0 cut_at = 0 while used < length and not ss.eos? out << ss.getch c = ss.scan(ANSIColorRegexp) out << c unless c.nil? used += 1 cut_at = out.length if used == cut_length end if not ss.eos? out = out[0, cut_at] out << omission end out end |
#whackuum(str = /\s+/) ⇒ Object
10 11 12 |
# File 'lib/muflax/string.rb', line 10 def whackuum str=/\s+/ self.split(str).map(&:strip) end |