instance_eval & instance_exec
class A
def initialize(x,y)
@x = x
@y = y
end
end
</pre>
</code>
obj = A.new('rays', 'rashmi')
obj.instanceeval{puts "Hello #{@x} #{@y}"} => Hello rays rashmi
</code>
</pre>
obj.instanceexec("Hello"){|arg| puts "#{arg} #{@x} #{@y} "}
=> Hello rays rashmi
</code>
</pre>
So we can't pass argument to block in instanceeval but can pass instanceexec. instance_exec is introduced in ruby 1.9.
This the only difference in instanceeval and instanceexec.
This feature has been backported to ruby 1.8.7. You can also test this in ruby 1.8.7
Written by Rashmi
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#