Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/extentions.rb
Instance Method Summary collapse
Instance Method Details
#split_by_width(width) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/extentions.rb', line 6 def split_by_width(width) cnt = 0 str = '' chunks = [] each_char do |c| if c == "\n" chunks << str str = '' cnt = 0 next end cnt += c.width if cnt > width chunks << str str = '' cnt = 0 end str << c unless str.empty? && c == ' ' end chunks << str unless str.empty? chunks end |
#width ⇒ Object
2 3 4 |
# File 'lib/extentions.rb', line 2 def width each_char.map { |c| c.bytesize == 1 ? 1 : 2 }.reduce(0, &:+) end |