Method: Translatomatic::String#substrings

Defined in:
lib/translatomatic/string.rb

#substrings(regex) ⇒ Array<Translatomatic::String] List of substrings

Find all substrings matching the given regex

Returns:



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/translatomatic/string.rb', line 72

def substrings(regex)
  matches = matches(@value, regex)
  strings = []
  matches.each do |match|
    substring = match.to_s
    # find leading and trailing whitespace
    next if substring.length == 0

    parts = substring.match(/\A(\s*)(.*?)(\s*)\z/m).to_a
    value = parts[2]
    offset = match.offset(0)[0]
    offset += parts[1].length  # leading whitespace
    strings << self.class.new(value, locale, offset: offset, parent: self)
  end

  # return [self] if there's only one substring and it's equal to self
  strings.length == 1 && strings[0].eql?(self) ? [self] : strings
end