I have class (highly simplified for the sake of this discussion)
class MyClass
def initialize
@myrecs = %w(a b c d)
end
def each_rec
@myrecs.each {|r| yield(r)}
end
end
and use it as
x = MyClass.new
x.each_rec { |r| .... }
Since my method each_rec basically does just a myrecs.each, I wanted to define somehow that each_rec just forwards to each. I can achieve the desired effect by explicitly passing the block, i.e.
def each_rec(&block)
@myrecs.each(&block)
end
but I wonder wether it is also possible to achieve my goal without having the block as explicit parameter. I tried the following approaches without success:
(1) Exploit the fact that each returns an Enumerator when called without a block:
def each_rec
@myrecs.each
end
(2) Create an Enumerator:
def each_rec
@myrecs.enum_for(:each)
end
In both cases, I did not get an error, but the block passed to each_rec was simply not entered.