Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/split_with_display_width.rb

Instance Method Summary collapse

Instance Method Details

#split_with_display_width_from_end(num) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/split_with_display_width.rb', line 21

def split_with_display_width_from_end(num)
  return ['', self] if num >= self.display_width

  first = []
  second = []
  self.each_char.to_a.reverse.each do |char|
    tmp = second + [char]
    if tmp.join.display_width <= num
      second << char
    else
      first << char
    end
  end

  [first.reverse.join, second.reverse.join]
end

#split_with_display_width_from_start(num) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/split_with_display_width.rb', line 4

def split_with_display_width_from_start(num)
  return [self, ''] if num >= self.display_width

  first = []
  second = []
  self.each_char.each do |char|
    tmp = first + [char]
    if tmp.join.display_width <= num
      first << char
    else
      second << char
    end
  end

  [first.join, second.join]
end