Class: R::RubyCallback

Inherits:
Object
  • Object
show all
Defined in:
lib/R_interface/ruby_callback.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ RubyCallback


Initializes a callback object and constructs the R function that calls back the object. RubyCallback class will act as proxy to the actual Ruby Object since it needs to deal with R parameters and Return values boxing and unboxing when needed


Parameters:

  • object (Object)

    Ruby Object



48
49
50
51
52
53
54
# File 'lib/R_interface/ruby_callback.rb', line 48

def initialize(object)
  @object = object
  
  # ruby_callback_method is a method that returns an R function that returns an R
  # function that calls back this object callback method (look at callback bellow)
  @r_function = R::Support.ruby_callback_method.call(method(:callback))
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args) ⇒ Object





60
61
62
# File 'lib/R_interface/ruby_callback.rb', line 60

def method_missing(symbol, *args)
  p "in ruby_callback.rb method missing with #{symbol} #{args}"
end

Instance Attribute Details

#objectObject (readonly)

The Ruby Proc, Method (or Object?) to be called back



29
30
31
# File 'lib/R_interface/ruby_callback.rb', line 29

def object
  @object
end

#r_functionObject (readonly)

The R function that will call back on the object



31
32
33
# File 'lib/R_interface/ruby_callback.rb', line 31

def r_function
  @r_function
end

Class Method Details

.build(object) ⇒ Object





37
38
39
# File 'lib/R_interface/ruby_callback.rb', line 37

def self.build(object)
  RubyCallback.new(object).r_function
end

Instance Method Details

#callback(*args) ⇒ Object





68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/R_interface/ruby_callback.rb', line 68

def callback(*args)

  # converts every arg into a R::Object (Ruby object that wraps an R Interop)
  args.map! { |arg| R::Object.build(arg) }
  
  # calls the callback method and convert the result back to an R object
  # method parse_arg was developed to parse the arguments to an R function
  # but in a callback the return value needs to be converted.  In this case
  # the name parse_arg is misleading
  R::Support.parse_arg(@object.call(*args))
  
end