Flowdock
method

instance_exec

Importance_1
Ruby latest stable (v1_9_3_392) - 0 notes - Class: BasicObject
instance_exec(*args) public

Executes 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. Arguments are passed as block parameters.

class KlassWithSecret
  def initialize
    @secret = 99
  end
end
k = KlassWithSecret.new
k.instance_exec(5) {|x| @secret+x }   #=> 104
Show source
Register or log in to add new notes.