1.9.2p290 :001 > def test
1.9.2p290 :002?> if block_given?
1.9.2p290 :003?> puts "I have a block"
1.9.2p290 :004?> yield
1.9.2p290 :005?> else
1.9.2p290 :006 > puts "no block"
1.9.2p290 :007?> end
1.9.2p290 :008?> end
=> nil
1.9.2p290 :011 > def other &blk
1.9.2p290 :012?> puts "passing block"
1.9.2p290 :013?> test &blk
1.9.2p290 :014?> end
=> nil
1.9.2p290 :015 > other
passing block
no block
=> nil
1.9.2p290 :018 > other {puts "I'm the block"}
passing block
I have a block
I'm the block
=> nil
Jesus.
···
On Tue, Sep 11, 2012 at 12:31 PM, bernhard stoecker <lists@ruby-forum.com> wrote:
hi @ all,
lets assume I have two methods in class Func
class Func
def self.mean(array)
return 0 if(array.size == 0)
if(block_given?)
return array.map{|x| yield(x)}.sum.to_f / array.size
end
return array.sum.to_f / array.size
end
def self.variance(array)
return 0 if array.size == 0
avr = Func.mean(array) #HOW TO HAND OVER A BLOCK???