Class: Rri::RConverters::ArrayConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/rri/r_converters/array_converter.rb

Overview

Converter for ruby Arrays

Instance Method Summary collapse

Instance Method Details

#convert(obj) ⇒ Array

Convert ruby object to R format

If the ruby object is an Array, converts it into an R object.

Does not deal with empty arrays.

Depending on what type of content the ruby array has, different things happen:

  • if all elements are doubles, convert to vector of doubles
  • if all elements are integers, convert to vector of integers
  • if all elements are strings, convert to vector of strings

Parameters:

  • obj

    object to convert

Returns:

  • (Array)

    an array of size 2 where first element is a boolean indicating succes, and the second element is the converted object if conversion successful



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rri/r_converters/array_converter.rb', line 23

def convert(obj)
  if obj.kind_of? Array and obj.size > 0
    if is_all_same_type?(obj)
      o = obj[0]
      return [true, create_integer_vector(obj)] if o.kind_of? Integer
      return [true, create_double_vector(obj)] if o.kind_of? Float
      return [true, create_string_vector(obj)] if o.kind_of? String
      [false, nil]
    end
  else
    [false, nil]
  end
end