method
instance_eval
Ruby latest stable (v2_5_5)
-
0 notes -
Class: BasicObject
- 1_8_6_287
- 1_8_7_72
- 1_8_7_330
- 1_9_1_378 (0)
- 1_9_2_180 (0)
- 1_9_3_125 (0)
- 1_9_3_392 (0)
- 2_1_10 (0)
- 2_2_9 (38)
- 2_4_6 (0)
- 2_5_5 (0)
- 2_6_3 (0)
- What's this?
instance_eval(*args)
public
Evaluates a string containing Ruby source code, or the given block, within the context of the receiver (obj). In order to set the context, the variable self is set to obj while the code is executing, giving the code access to obj’s instance variables and private methods.
When instance_eval is given a block, obj is also passed in as the block’s only argument.
When instance_eval is given a String, the optional second and third parameters supply a filename and starting line number that are used when reporting compilation errors.
class KlassWithSecret def initialize @secret = 99 end private def the_secret "Ssssh! The secret is #{@secret}." end end k = KlassWithSecret.new k.instance_eval { @secret } #=> 99 k.instance_eval { the_secret } #=> "Ssssh! The secret is 99." k.instance_eval {|obj| obj == self } #=> true