Skip to content

Instantly share code, notes, and snippets.

@humbroll
Created October 9, 2013 19:53
Show Gist options
  • Save humbroll/2b82e525a3c6af61c7ea to your computer and use it in GitHub Desktop.
Save humbroll/2b82e525a3c6af61c7ea to your computer and use it in GitHub Desktop.
binding() method - Ruby reflection -
class Girl
attr_accessor :age, :height
def initialize(a, h)
@age = a
@height = h
end
def getBinding
return binding()# a method defined in Kernel module
end
def to_s
"Age : #{@age}, Height: #{@height}"
end
end
girlfriend = Girl.new(25, 160)
puts girlfriend
#get the value of the instance variable @age stored in the binding of object girlfriend
eval("@age -= 1", girlfriend.getBinding)
eval("@height += 3", girlfriend.getBinding)
puts girlfriend
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment