Module: StringHelper

Included in:
Cuker::JiraModel, Cuker::RubyXLModel
Defined in:
lib/cuker/helpers/string_helper.rb

Instance Method Summary collapse

Instance Method Details

#add_newlines(str, max_len) ⇒ Object



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

def add_newlines(str, max_len)
  words = str.split(' ')
  lines = []
  current_line = []
  current_len = 0
  until words.empty?
    next_word = words.shift
    current_line << next_word
    current_len += next_word.length + 1
    if current_len >= max_len
      lines << current_line.join(' ')
      current_line = []
      current_len = 0
    end
  end
  lines << current_line.join(' ') unless current_line.empty?
  lines.join("\n")
end

#add_newlines!(str, max_len) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/cuker/helpers/string_helper.rb', line 7

def add_newlines!(str, max_len)
  index = max_len - 1
  until index >= str.length
    if str[index] == ' '
      str[index] = "\n"
      index += max_len
    else
      index += 1
    end
  end
  str
end

#add_newlines_regex(str, max_len) ⇒ Object



3
4
5
# File 'lib/cuker/helpers/string_helper.rb', line 3

def add_newlines_regex(str, max_len)
  str.scan(/.{1,#{max_len}}.*?(?:\b|$)/).join "\n"
end