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

#blank?Boolean

blank line?

Returns:

  • (Boolean)


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

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

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

comment line?

Parameters:

  • char (String) (defaults to: '#')

    line beginning char

Returns:

  • (Boolean)


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

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

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

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]

Parameters:

  • delim_regex (Regex)

    regular expression for split

  • quote_char (String) (defaults to: "'")

    quote char such as ' and "

Returns:



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