Last Updated: February 25, 2016
·
2.688K
· raysrashmi

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