Delegating each

I have a class that basically encapsulates an array and mixes
Enumerable in. Currently it looks something like this

class C
include Enumerable

def each(&proc)
@myarray.each do |e|
proc.call(e)
end
end
end

Is there a more direct way of implementing each? Is is possible to get
rid of the each-loop and delegate its behavior to myarray?

Michael

···


Michael Schuerig If at first you don’t succeed…
mailto:schuerig@acm.org try, try again.
http://www.schuerig.de/michael/ --Jerome Morrow, “Gattaca”

Michael Schuerig schuerig@acm.org writes:

class C
include Enumerable

def each(&proc)
@myarray.each do |e|
proc.call(e)
end
end
end

Is there a more direct way of implementing each? Is is possible to get
rid of the each-loop and delegate its behavior to myarray?

def each(&block)
@myarray.each(&block)
end

Cheers

Dave

[…]

Is there a more direct way of implementing each? Is is possible to get
rid of the each-loop and delegate its behavior to myarray?

You could try …

def each(&block)
@myarray.each(&block)
end

···

On Sat, 2002-10-12 at 19:14, Michael Schuerig wrote:


– Jim Weirich jweirich@one.net http://w3.one.net/~jweirich

“Beware of bugs in the above code; I have only proved it correct,
not tried it.” – Donald Knuth (in a memo to Peter van Emde Boas)

I have a class that basically encapsulates an array and mixes
Enumerable in. Currently it looks something like this

class C
include Enumerable

def each(&proc)
@myarray.each do |e|
proc.call(e)
end
end
end

Is there a more direct way of implementing each? Is is possible to get
rid of the each-loop and delegate its behavior to myarray?

Michael

In addition to the other replies, you can check out the Delegate and
SimpleDelegate classes. I haven’t used these, so I can’t help more, but these
will be useful if you want to delegate other methods as well.

Gavin

···

From: “Michael Schuerig” schuerig@acm.org

require ‘forwardable’

class C
extend Forwardable
def_delegators(:@myarray, :each)
end

Massimiliano

···

On Sun, Oct 13, 2002 at 08:14:20AM +0900, Michael Schuerig wrote:

Is there a more direct way of implementing each? Is is possible to get
rid of the each-loop and delegate its behavior to myarray?

Or, if you expose a bunch of the array’s functionality,

class C

def method_missing(method_name, *args)
@myarray.send(method_name, *args)
end

end

···

Michael Schuerig (schuerig@acm.org) wrote:

I have a class that basically encapsulates an array and mixes
Enumerable in. Currently it looks something like this

class C
include Enumerable

def each(&proc)
@myarray.each do |e|
proc.call(e)
end
end
end

Is there a more direct way of implementing each? Is is possible to get
rid of the each-loop and delegate its behavior to myarray?


Eric Hodel - drbrain@segment7.net - http://segment7.net
All messages signed with fingerprint:
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04

Dave Thomas wrote:

Michael Schuerig schuerig@acm.org writes:
[snip]

Is there a more direct way of implementing each? Is is possible to
get rid of the each-loop and delegate its behavior to myarray?

def each(&block)
@myarray.each(&block)
end

Sigh… I need to go to bed.

Thanks!

Michael

···


Michael Schuerig If at first you don’t succeed…
mailto:schuerig@acm.org try, try again.
Michael Schürig | Sentenced to making sense --Jerome Morrow, “Gattaca”

Massimiliano Mirra wrote:

···

On Sun, Oct 13, 2002 at 08:14:20AM +0900, Michael Schuerig wrote:

Is there a more direct way of implementing each? Is is possible to
get rid of the each-loop and delegate its behavior to myarray?

require ‘forwardable’

class C
extend Forwardable
def_delegators(:@myarray, :each)
end

That’s cool. I didn’t even know Forwardable existed.

Michael


Michael Schuerig If at first you don’t succeed…
mailto:schuerig@acm.org try, try again.
Michael Schürig | Sentenced to making sense --Jerome Morrow, “Gattaca”