Class: R::Vector

Inherits:
Object show all
Includes:
Enumerable, BinaryOperators, ExecBinOp, ExecUniOp, IndexedObject, LogicalOperators, UnaryOperators
Defined in:
lib/R_interface/rvector.rb

Instance Attribute Summary

Attributes inherited from Object

#r_interop, #statement

Instance Method Summary collapse

Methods included from LogicalOperators

#&, #|

Methods included from ExecUniOp

#exec_uni_oper

Methods included from UnaryOperators

#!, #+@, #-@

Methods included from ExecBinOp

#coerce, #exec_bin_oper

Methods included from BinaryOperators

#!=, #%, #*, #**, #+, #-, #/, #<, #<=, #>, #>=, #eq, #int_div, #til

Methods included from IndexedObject

#[], #[]=, #size

Methods inherited from Object

#==, #_, #attr=, build, #comment, #comment=, #dim, #dim=, #dimnames, #dimnames=, #eql, #method_missing, #names, #names=, #pp, #pretty_print, #rclass, #rclass=, #row__names, #row__names=, #setR, #setR_name, #to_s, #tsp, #tsp=

Constructor Details

#initialize(r_interop) ⇒ Vector





39
40
41
# File 'lib/R_interface/rvector.rb', line 39

def initialize(r_interop)
  super(r_interop)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class R::Object

Instance Method Details

#<=>(other_vector) ⇒ Object


@TODO: SHOULD DEFINE COMPARISON BETWEEN TWO VECTORS




114
115
116
# File 'lib/R_interface/rvector.rb', line 114

def <=>(other_vector)
  puts "comparison called"
end

#>>(index) ⇒ Object


When indexing with ‘[’ or ‘[[’ an R object is returned. Sometimes we need to have access to an umboxed Ruby element, for instance, in an numeric array, we might want to receive the actual number that can be used in a Ruby method. In this case, we use the ‘<<’ operator.


Returns:

  • the Ruby element at the given index in the vector

Raises:

  • (IndexError)


51
52
53
54
55
# File 'lib/R_interface/rvector.rb', line 51

def >>(index)
  raise IndexError.new("index #{index} out of array bounds: -#{index - 1}...#{index - 1}") if
    (index >= @r_interop.size) 
  @r_interop[index]
end

#each(result = :vec) ⇒ Object


Each cannot return a Enumerator because R is single threaded. When this restriction is removed, make each return self.to_enum




71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/R_interface/rvector.rb', line 71

def each(result = :vec)

  case result
  when :vec
    # length is a R::Vector, in order to extract its size as a Numeric we need to
    # use the >> operator
    (1..length >> 0).each do |i|
      yield self[i]
    end
  when :native
    (0...length >> 0).each do |i|
      yield self >> i
    end
  else
    raise "Type #{result} is unknown for method :each"
  end
  
end

#each_with_index(result = :vec) ⇒ Object


Need to override each_with_index, as R indexing starts at 1




94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/R_interface/rvector.rb', line 94

def each_with_index(result = :vec)
  case result
  when :vec
    (1..length >> 0).each do |i|
      yield self[i], i
    end
  when :native
    (0...length >> 0).each do |i|
      yield self >> i, i
    end
  else
    raise "Type #{result} is unknown for method :each"
  end
  
end

#popObject





61
62
63
# File 'lib/R_interface/rvector.rb', line 61

def pop
  self >> 0
end