Created
October 9, 2013 19:53
-
-
Save humbroll/2b82e525a3c6af61c7ea to your computer and use it in GitHub Desktop.
binding() method - Ruby reflection -
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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