Class: Array

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

Overview

Extension to the Array class. These facilitate the creation of RTPConnect strings from an array of values.

Instance Method Summary collapse

Instance Method Details

#encodeString

Encodes an RTPConnect string from an array of values. Each value in the array is wrapped with double quotes, before the values are joined with a comma separator.

Returns:

  • (String)

    a proper RTPConnect type CSV string



95
96
97
98
# File 'lib/rtp-connect/ruby_extensions.rb', line 95

def encode
  wrapped = self.collect{|value| value.wrap}
  return wrapped.join(',')
end

#validate_and_process(nr) ⇒ Object

Validates the number of elements in an array and converts all elements to strings.

Parameters:

  • nr (Integer)

    the required number of elements in the array

Raises:

  • (ArgumentError)


105
106
107
108
# File 'lib/rtp-connect/ruby_extensions.rb', line 105

def validate_and_process(nr)
  raise ArgumentError, "Invalid array length. Expected exactly #{nr} elements, got #{self.length}." unless self.length == nr
  self.collect {|e| e && e.to_s.strip}
end