Error
  • JHTMLicon not supported. File not found.
  • JHTMLicon not supported. File not found.
  • JHTMLicon not supported. File not found.

eval

Using Kernel#binding to capture variable values from a method
Friday, 18 June 2010 20:45
To use binding to capture variable values from a method, add a binding statement inside the method and then refer to the method from an eval statement on the outside e.g.

class A
def initialize()
end

def f
a = 22
b = 33
binding
end

def b
vars = eval "local_variables", f
out = vars.map {|x| eval x, f}
puts vars.zip(out).map{|x| "%s: %s" % x}.join("\n")
end
end
A.new.b


#=>
a: 22
b: 33

Resources:

- Module: Kernel [ruby-doc.org]
- { |one, step, back| } [onestepback.org]

Read more: http://feeds.dzone.com/~r/dzone/snippets/~3/YmusY9n2gBI/11683

 


Taxonomy by Zaragoza Online