Markus Jais <info / mjais.de> wrote in message news:<bcqr0s$m8dq4$1 / ID-75083.news.dfncis.de>... > hello > > > at the end is a sample code > this does not work as I want. I want to use each_if > to yield only the elements that support a certain criteria and then > do something with this elements. but > here the output is: > > ./bl.rb > e: 5 > e: 8 > e: 9 > 2 > 4 > 5 > 8 > 9 > > I do not want the numbers 2 and 4 to be printed > > I could save the elements in an array and return it but I was wondering if > there is another possibility > > Markus > > class Array > > def each_if(&b) > each do |e| > if b.call(e) > print "e: ", e, "\n" > yield e > end > end > end > > end > > > a = [2, 4, 5, 8, 9] > > a.each_if { |x| x > 4 }.each { |e| puts e } Just some comments to explain the result you obtain : the each method returns always the calling object, so your each_if returns also the calling object a.each_if{ |x| x > 4 }.each { |e| puts e } is same as a.each { |e| puts e } Thats why you get all the numbers printed out. Concerning the proc's that are passed to a method: def each_if(&b) ... end means that 'b.call' is the same as 'yield'. In your case you need two procs so you have to pass one explicitely in a parameter (without &). Ciao