Blake Hilscher

Thoughts on ruby development.

Invoke a Proc in the Context of Another Object.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class ObjectContext

  def initialize
    @number = 10
  end

  def evaluate(proc)
    instance_exec &proc
  end

end

# our multiplication proc
t = -> { puts( @number * @number) }

# evaluate in current context
@number = 5
t.call #=> 25 

# evaluate in context of an ObjectFactory instance 
o = ObjectContext.new
o.evaluate(t) #=> 100