Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/fselector/util.rb
Overview
add functions to String class
Instance Method Summary collapse
-
#blank? ⇒ Boolean
blank line?.
-
#comment?(char = '#') ⇒ Boolean
comment line?.
-
#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.
Instance Method Details
#blank? ⇒ Boolean
blank line?
132 133 134 |
# File 'lib/fselector/util.rb', line 132 def blank? return self =~ /^\s*$/ end |
#comment?(char = '#') ⇒ Boolean
comment line?
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]
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 |