ruby - Get the calling object of a block -
if have class object1 method method1 , "global" method2 inside kernel module.
method1 run block transforming proc , call it.
if have :
obj1 = new object1 obj1.method1(arg1,arg2){ method2() } is possible in method2 know obj1 called block ?
assume want method2 global method. get the access calling object passing binding. more simpler way pass caller object it:
example:
def method2(o) #puts this.object_id puts "inside method2()" puts "#{o.object_id}" end class object1 def method1 method2(self) end end obj1 = object1.new puts obj1.object_id obj1.method1() output:
23644040 inside method2() 23644040
Comments
Post a Comment