Hi,

In message "Re: Proc as a parameter in a class"
    on Tue, 9 Aug 2005 06:21:08 +0900, "Yannick Turgeon" <vendredi5h / gmail.com> writes:

|---------------------------------
|##### FIRST SECTION #####
|puts "First section"
|
|def fn(text)
|    return proc{|factor| puts text * factor}
|end
|
|f = fn("Joe ")
|f.call(3)       #It prints "Joe Joe Joe ", as expected.

|##### SECOND SECTION #####
|puts "Second section"
|
|class Bob
|    def initialize(factor, &block)
|        @factor = factor
|        @action = block
|    end
|
|    def run
|        @action.call(@factor)
|    end
|end
|
|b = Bob.new(3){f}
|b.run         #It prints nothing [b.run return the function returned by fn()]

Just because you've specified so.  @action is {f} which means if you
call, it evaluates the variable reference f and gives you its value.

|##### THIRD SECTION #####
|puts "Third section"
|
|#Redefine run()
|class Bob
|    def run
|        @action.call.call(@factor)      #Why should I call "call" twice. Isn't @action supposed to be the function returned by fn()?
|    end
|end
|
|b = Bob.new(3){f}
|b.run()         #It prints "Joe Joe Joe ".

Just because you've specified so.  Since @action is {f} as above, the
first "call" gives you f, you have to call it again to invoke the f
"function".

							matz.