Class: R::SymbolRef

Inherits:
Object
  • Object
show all
Includes:
BinaryOperators, ExpBinOp, LogicalOperators
Defined in:
lib/R_interface/ruby_extensions.rb

Constant Summary collapse

REF_RUBY_RESERVED =
%i[
  to_str to_path to_ary to_int to_f to_r to_proc to_hash to_h to_a call
].freeze

Instance Method Summary collapse

Methods included from LogicalOperators

#&, #|

Methods included from ExpBinOp

#assign, #coerce, #exec_bin_oper, #inter

Methods included from BinaryOperators

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

Constructor Details

#initialize(name) ⇒ SymbolRef

Returns a new instance of SymbolRef.



48
49
50
# File 'lib/R_interface/ruby_extensions.rb', line 48

def initialize(name)
  @name = name
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/R_interface/ruby_extensions.rb', line 73

def method_missing(method_name, *args)
  super if REF_RUBY_RESERVED.include?(method_name)
  name = R::Support.convert_symbol2r(method_name)
  r_args = [self, *args].map { |arg| R::Support.parse_arg(arg) }
  expr = "#{name}(#{r_args.join(", ")})"
  res = R::Language.allocate
  res.instance_variable_set(:@r_interop, expr)
  res.expression = expr
  res
end

Instance Method Details

#+@Object



56
57
58
59
60
# File 'lib/R_interface/ruby_extensions.rb', line 56

def +@
  var_name = R::Support.generate_var_name
  R.bridge.eval_r("#{var_name} <- as.name('#{to_r_symbol}')")
  R::Object.build(var_name)
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
87
88
# File 'lib/R_interface/ruby_extensions.rb', line 84

def respond_to_missing?(method_name, include_private = false)
  return false if method_name == :r_interop || method_name == :expression
  return false if REF_RUBY_RESERVED.include?(method_name)
  true
end

#to_r_symbolObject



52
53
54
# File 'lib/R_interface/ruby_extensions.rb', line 52

def to_r_symbol
  @name.to_s.gsub(/__/, ".")
end

#up_to(other_object) ⇒ Object



69
70
71
# File 'lib/R_interface/ruby_extensions.rb', line 69

def up_to(other_object)
  R::Language.build("`:`", self, other_object)
end

#~@Object



62
63
64
65
66
67
# File 'lib/R_interface/ruby_extensions.rb', line 62

def ~@
  expr = to_r_symbol
  var_name = R::Support.generate_var_name
  R.bridge.eval_r("#{var_name} <- #{expr}")
  R::Object.build(var_name)
end