[LRUG] Getting rid of 'proc'

Paul Battley pbattley at gmail.com
Wed Apr 18 03:13:58 PDT 2007


On 18/04/07, Paolo Dona <paolo.dona at seesaw.it> wrote:
> Hi Paul, your sample seems good, but what I'm trying to do with tabnav is a
> little bit more complicated.
>
> You're able to define the block do p @foo end because @foo is an instance
> variable of your class A (that could correspond to the tabnav model) but
> inside the tabnav I should reference something that exists only in the
> views's context, not the model.. imagine to evaluate a @foo that's inside
> another class..

Paolo, does this do what you expect, or am I missing something?

---

#!/usr/bin/env ruby

class A
 def define(&blk)
   @fn = blk
 end

 def evaluate(context)
   context.instance_eval(&@fn)
 end
end

class B
  def initialize
    @foo = 42
  end
end

a = A.new
a.define do
 p @foo
end
b = B.new
a.evaluate(b) # => 42

---

Paul.



More information about the Chat mailing list