class String

Changes:

  1. Now using a more recent version from ActiveSupport. This more recent version from ActiveSupport is likely more efficient and slightly more succinct.

Changes since 0.6:

  1. + row_separator.

Changes since 0.0

  1. Updated with changes from active_support 3.0.0.

  2. Will now successfully handle strings with spaces.

  3. Removed the dup'ed word since once the transformational methods were chained this became unnecessary.

Public Instance Methods

blank?() click to toggle source
# File lib/String/blankQ.rb, line 14
def blank?
  self !~ /\S/
end
split_csv(quote = nil, column_separator = ',', row_separator = nil) click to toggle source
# File lib/String/split_csv.rb, line 20
def split_csv(quote = nil, column_separator = ',', row_separator = nil)
  case quote
  when :none, :unquoted
    if row_separator
      self.chomp(row_separator).split(column_separator)
    else
      self.chomp.split(column_separator)
    end
  when :double, :double_quoted, :double_quotes
    if row_separator
      self.chomp(row_separator).split(column_separator).collect{|e| e.sub(/^"/, '').sub(/"$/, '')}
    else
      self.chomp.split(column_separator).collect{|e| e.sub(/^"/, '').sub(/"$/, '')}
    end
  else
    split_row = []
    assembling_column = false
    buffer = ''
    if row_separator
      self.chomp!(row_separator)
    else
      self.chomp!
    end
    self.split(column_separator).each do |e|
      if assembling_column && !(e =~ /"$/) # e.not_closing_quotes?
        buffer << e
      elsif assembling_column && e =~ /"$/ # e.closing_quotes?
        buffer << e.sub(/"$/, '') # remove the trailing quote
        split_row << buffer
        assembling_column = false
      elsif (e =~ /^"/) && !(e =~ /"$/) # e.opening_quotes_but_not_closing_quotes?
        buffer = ''
        buffer << e.sub(/^"/, '') + column_separator # remove leading quote and replace the column_separator
        assembling_column = true
      else
        if (e =~ /^"/) && (e =~ /"$/) # e.both_opening_and_closing_quotes?
          split_row << e.sub(/^"/, '').sub(/"$/, '')
        else
          split_row << e
        end
      end
    end
    split_row
  end
end
underscore() click to toggle source
# File lib/String/underscore.rb, line 23
def underscore
  gsub(' ', '_').
  gsub(/::/, '/').
  gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
  gsub(/([a-z\d])([A-Z])/,'\1_\2').
  tr("-", "_").
  downcase
end