Passing on a block

I defined a Statespace class, which happens to encode states as integers
in a range 0…nstates.
I want to provide an iterator like

def each
nstates.times { |i| yield i }
end

or i could write it as

def each(&b)
nstates.times(&b)
end

Which is the better or more Ruby-esque way?
Any difference in efficiency?

regards,
-John

I would take the latter because of does not hard codes the arity of
#times.

-billy.

···

On Tue, Oct 15, 2002 at 03:18:29AM +0900, John Tromp wrote:

I defined a Statespace class, which happens to encode states as integers
in a range 0…nstates.
I want to provide an iterator like

def each
nstates.times { |i| yield i }
end

or i could write it as

def each(&b)
nstates.times(&b)
end

Which is the better or more Ruby-esque way?
Any difference in efficiency?


Meisterbohne Söflinger Straße 100 Tel: +49-731-399 499-0
eLösungen 89077 Ulm Fax: +49-731-399 499-9

Philipp Meier wrote:

def each(&b)
nstates.times(&b)
end

Which is the better or more Ruby-esque way?
Any difference in efficiency?

I would take the latter because of does not hard codes the arity of
#times.

I thought of method arity when I first read your sentence, so just to be
terribly pedantic, I’d suggest:
def each(*args,&b)
nstates.times(*args,&b)
end

···

On Tue, Oct 15, 2002 at 03:18:29AM +0900, John Tromp wrote:


([ Kent Dahl ]/)_ ~ [ http://www.stud.ntnu.no/~kentda/ ]/~
))_student
/(( _d L b_/ NTNU - graduate engineering - 5. year )
( __õ|õ// ) )Industrial economics and technological management(
_
/ö____/ (_engineering.discipline=Computer::Technology)