Sha256: 5e9b9c2584e9fa2375f2aae2c5e85b483cc69005c10c3cc0613ef7429caac6e3

Contents?: true

Size: 865 Bytes

Versions: 9

Compression:

Stored size: 865 Bytes

Contents

module StringHelper

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

  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

  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

end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
cuker-0.6.7 lib/cuker/helpers/string_helper.rb
cuker-0.6.6 lib/cuker/helpers/string_helper.rb
cuker-0.6.5 lib/cuker/helpers/string_helper.rb
cuker-0.5.22 lib/cuker/helpers/string_helper.rb
cuker-0.5.21 lib/cuker/helpers/string_helper.rb
cuker-0.5.15 lib/cuker/helpers/string_helper.rb
cuker-0.5.7 lib/cuker/helpers/string_helper.rb
cuker-0.5.3 lib/cuker/helpers/string_helper.rb
cuker-0.4.9 lib/cuker/helpers/string_helper.rb