Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/rtp-connect/ruby_extensions.rb

Overview

Extension to the String class. These facilitate processing and analysis of RTPConnect strings.

Instance Method Summary collapse

Instance Method Details

#checksumFixnum

Determines the checksum (CRC) for a given string.



13
14
15
16
17
18
19
# File 'lib/rtp-connect/ruby_extensions.rb', line 13

def checksum
  crc = RTP::CRC_SEED
  self.each_codepoint do |byte|
    crc = RTP::CRC_TABLE[(crc ^ byte) & 0xff] ^ (crc >> 8)
  end
  return crc
end

#elementsArray<String>

Splits the elements of a string separated by comma.



25
26
27
# File 'lib/rtp-connect/ruby_extensions.rb', line 25

def elements
  self.split(',')
end

#valueString

Removes leading & trailing double quotes from a string.



33
34
35
# File 'lib/rtp-connect/ruby_extensions.rb', line 33

def value
  self.gsub(/\A"|"\Z/, '')
end

#valuesArray<String>

Splits the elements of a CSV string (comma separated values) and removes quotation (leading and trailing double-quote characters) from the extracted string elements.



43
44
45
46
47
48
49
50
# File 'lib/rtp-connect/ruby_extensions.rb', line 43

def values
  begin
    CSV.parse(self).first
  rescue StandardError => e
    RTP.logger.error("Unable to parse the given string record. Probably invalid CSV format: #{self}")
    raise e
  end
end

#wrapString

Wraps double quotes around the string.



56
57
58
# File 'lib/rtp-connect/ruby_extensions.rb', line 56

def wrap
  '"' + self + '"'
end