[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Redefe each to include each_with_index and arity=2

Dan Diebolt

9/10/2008 6:59:00 PM

> ... each_with_index and each depend on each other
=A0
Maybe someone with more knowlege can track this dependance down. I am keen =
to learn, but I have to admit that=A0I am stretching my understanding of th=
e Ruby language here.
=A0
I would like to ask others if they think having each overloaded to take eit=
her |item| or |item,index| would be a general language improvement. Also, f=
or greatest generality should this proposed redefinition of each be made to=
the class Array or to the enumerator mixin or somewhere else?
2 Answers

Anton Ivanov

9/10/2008 8:01:00 PM

0


> I would like to ask others if they think having each overloaded to take
> either |item| or |item,index| would be a general language improvement.
> Also, for greatest generality should this proposed redefinition of each
> be made to the class Array or to the enumerator mixin or somewhere else?

Like you, I've wondered why each could not do both things. On the one
hand it would be nice if it supported the index functionality. However,
it is really a breaking change. Consider what a and b are assigned
currently at each iteration of the following, and what they would be
assigned under your proposal.

[[1,2], [3,4]].each {|a,b|}

To answer your second question, each() must know the details of the
container, and so it cannot be in a mixin. The Enumerable mixin defines
functionality in terms of 'each' which must be supplied by the class
into which you mix it in.
--
Posted via http://www.ruby-....

Trans

9/11/2008 12:49:00 AM

0



On Sep 10, 2:59=A0pm, "DanDiebolt.exe" <dandieb...@yahoo.com> wrote:
> > ... each_with_index and each depend on each other
>
> =A0
> Maybe someone with more knowlege can track this dependance down. I am kee=
n to learn, but I have to admit that=A0I am stretching my understanding of =
the Ruby language here.
> =A0
> I would like to ask others if they think having each overloaded to take e=
ither |item| or |item,index| would be a general language improvement. Also,=
for greatest generality should this proposed redefinition of each be made =
to the class Array or to the enumerator mixin or somewhere else?

Can't do it, it would break things.

I've always though an special 'it' object would be nice though. Eg.

[:a, :b, c:].each do |e|
it.index
it.first?
it.last?
end

T.