Hi,
I would like to know your opinion about the following idea's.
(It is actually are two RCR's)
(1)* Enumerable::filter and Enumerable::enum_filter
filter([[method = :each], *args, ] block)
yield values for which block returns true
3.filter(:times, &lambda{ |i| i[0] == 0 }) do |n|
puts "%i is even" % n
end
=> 2 is even
composers = %W(Brahms Berlioz Chopin)
composers.filter(&lambda { |i| i[0] == ?B }) do |c|
puts "composer with B: #{c}"
end
=> composer with B: Brahms
=> composer with B: Berlioz
and now the fun stuff (turning into an enum):
5.enum_filter(:times){ |i| i[0] == 0 }.collect
=> [2, 4]
(2)* Enumerable::Enumerator takes a optional block
powers = 4.enum_for(:times){ |i| i * i }
powers.collect
=> [0, 1, 4, 9]
Thanks,
Kristof