Class: String

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

Overview

add functions to String class

Instance Method Summary (collapse)

Instance Method Details

- (Boolean) blank?

blank line?



132
133
134
# File 'lib/fselector/util.rb', line 132

def blank?
  return self =~ /^\s*$/
end

- (Boolean) comment?(char = '#')

comment line?



126
127
128
# File 'lib/fselector/util.rb', line 126

def comment?(char='#')
  return self =~ /^#{char}/
end

- (Array<String>) split_me(delim_regex, quote_char = "'")

Enhanced String.split with escape char, which means string included in a pair of escape char is considered as a whole even if it matches the split regular expression. this is especially useful to parse CSV file that contains comma in a doube-quoted string e.g. 'a,"b, c",d'.split_me(/,/, '"') => [a, 'b, c', d]



148
149
150
151
152
153
154
155
# File 'lib/fselector/util.rb', line 148

def split_me(delim_regex, quote_char="'")
  d, q = delim_regex, quote_char
  if not self.count(q) % 2 == 0
    $stderr.puts "unpaired char of #{q} found, return nil"
    return nil
  end
  self.split(/#{d.source} (?=(?:[^#{q}]* #{q} [^#{q}]* #{q})* [^#{q}]*$) /x)
end