Delegating a method to a different object - style question

I have a class, and one of its instance variables is a hash. I want to
define a method “each” to my class, which simply executes an “each” to
this hash.

I can think of two possible solutions, which both work well. Assuming
that my hash is @myh, I can define my :each method as either

def each
    @myh.each { |k,v| yield(k,v) }


def each(&block)

My questions:

  • Does anybody know of a yet (fundamentally) different solution for this
    problem? I was thinking that the general pattern here would be “delegate
    a method call with all parameters to a different object”. This seems to
    be general enough, that I wonder, whether Ruby has a mechanism for this.

  • Thinking about maintainability, what are the possible disadvantages of
    these solutions? I don’t see any advantage of one over the other, but
    would appreciate other opinions.

BTW, I’m using Ruby 2.0, but solutions to more recent Ruby versions are
also appreciated.

Jesus Castello wrote in post #1175450:

Check out Forwadable. I believe this is what you are looking for :slight_smile:

That’s it exactly!!! Thanks a lot!!