Class: Proc

Inherits:
Object show all
Defined in:
lib/rubyvis/javascript_behaviour.rb

Overview

Add javascript-like apply and call methods to Proc, called js_apply and js_call, respectivly.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#orderObject

Used on Rubyvis::Nest



43
44
45
# File 'lib/rubyvis/javascript_behaviour.rb', line 43

def order
  @order
end

Instance Method Details

#js_apply(obj, args) ⇒ Object

Emulation of apply javascript method. apply has this signature

my_proc.apply(my_obj, args)

where

my_proc

a proc

my_obj

object inside proc is eval’ed

args

array of arguments for proc

apply on javascript is very flexible. Can accept more or less variables than explicitly defined parameters on lambda, so this method adds or remove elements according to lambda arity



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rubyvis/javascript_behaviour.rb', line 57

def js_apply(obj,args)
  arguments=args.dup
  # Modify numbers of args to works with arity
  min_args=self.arity > 0 ? self.arity : (-self.arity)-1
  
  if args.size > min_args and self.arity>0
    arguments=arguments[0,self.arity]
  elsif args.size < min_args
    arguments+=[nil]*(min_args-args.size)
  end
  #puts "#{args}->#{arguments} (#{self.arity})"
  if self.arity==0
      obj.instance_exec(&self)
  else
    obj.instance_exec(*arguments, &self)
  end
end

#js_call(obj, *args) ⇒ Object

Same as js_apply, but using explicit arguments



75
76
77
# File 'lib/rubyvis/javascript_behaviour.rb', line 75

def js_call(obj, *args)
  js_apply(obj,args)
end