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.

Returns:

  • (Fixnum)

    the checksum (a 16 bit unsigned integer)



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.

Returns:

  • (Array<String>)

    an array of the string values originally separated by a 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.

Returns:

  • (String)

    the processed 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.

Returns:

  • (Array<String>)

    an array of the comma separated values



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.

Returns:

  • (String)

    the string padded with double-quotes



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

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