From: "Jesús Gabriel y Galán" Date: 2012-09-11T19:51:38+09:00 Subject: Re: hand over block to a submethod On Tue, Sep 11, 2012 at 12:31 PM, bernhard stoecker 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??? > > if(block_given?) > return array.map { |i| (yield(i) - avr)**2 }.sum / array.size > end > return self.map { |i| (i - avr)**2 }.sum / self.size > > end > > end > > > my problem is, that i have to hand over the block given to variance to > the mean method. is this possible and if yes how? > > thanks in advance > > greets bernhard > > -- > Posted via http://www.ruby-forum.com/. > 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.