You should use instance_eval :
a.instance_eval { @var_one = 1 } => 1 a.instance_variables => [:@var_one]
When you use regular eval , you define your instance variable in the context of the current self , if you do this in irb, this is the main object:
a.eval { self } => main
So, you can change your a.eval method by executing a block in the instance context:
def a.eval(&block) instance_eval &block end a.eval { @a = 1 } => 1 a.instance_variables => [:@a]
source share