From: Bug Free on
Robert Dober wrote:
> I am afraid that is incorrect :(
> ruby -ve '[5, 7].each_with_index.each {|a| p a}'
> ruby 1.9.2dev (2010-05-31 revision 28117) [i686-linux]
> 5
> 7

However, with the each_cons we get:

ruby -ve '[5, 7].each_with_index.each_cons(2) {|a| p a}'
ruby 1.9.2dev (2010-05-31 revision 28117) [x86_64-linux]
[[5, 0], [7, 1]]
--
Posted via http://www.ruby-forum.com/.

From: Robert Dober on
On Tue, Jun 1, 2010 at 7:32 PM, Bug Free <amberarrow(a)yahoo.com> wrote:
> Robert Dober wrote:
>> I am afraid that is incorrect :(
>> ruby -ve '[5, 7].each_with_index.each {|a| p a}'
>> ruby 1.9.2dev (2010-05-31 revision 28117) [i686-linux]
>> 5
>> 7
>
> However, with the each_cons we get:
>
> ruby -ve '[5, 7].each_with_index.each_cons(2) {|a| p a}'
> ruby 1.9.2dev (2010-05-31 revision 28117) [x86_64-linux]
> [[5, 0], [7, 1]]
> --
Funny indeed...


--
The best way to predict the future is to invent it.
-- Alan Kay

From: botp on
On Wed, Jun 2, 2010 at 3:45 AM, Robert Dober <robert.dober(a)gmail.com> wrote:
> On Tue, Jun 1, 2010 at 7:32 PM, Bug Free <amberarrow(a)yahoo.com> wrote:
>> Robert Dober wrote:
>>> I am afraid that is incorrect :(
>>> ruby -ve '[5, 7].each_with_index.each {|a| p a}'
>>> ruby 1.9.2dev (2010-05-31 revision 28117) [i686-linux]
>>> 5
>>> 7
>>
>> However, with the each_cons we get:
>>
>> ruby -ve '[5, 7].each_with_index.each_cons(2) {|a| p a}'
>> ruby 1.9.2dev (2010-05-31 revision 28117) [x86_64-linux]
>> [[5, 0], [7, 1]]
>> --
> Funny indeed...

ah, ok, now i know where i'm confusing myself, and in turn, all of you :)
i've always thought that to capture the index of each_with_index (and
it's siblings) you'll have to explicitly specify the index or use the
splat op. this is because (my thoughts only emphasized again) ruby
will passed the whole args to the block splatted (w index as the last
arg), so you'll have to call the splat again to unsplat it. for
each_cons, the block arg is arrayed/unsplatted, so you get the whole
args. again, just my usual primitive thoughts. Works for me all the
time, and never thought about it again until now. As to whether i
should change the way i think about each_with*, no problem, this is
ruby :)

thanks and kind regards -botp

From: Robert Dober on
Do not be too harsh with yourself, the semantics of this are
convenient(IMHO) but not necessarily consistent with what one might
expect, if somebody made a CR for the behavior you were expecting I
would probably vote for it.
Cheers
R.

--
The best way to predict the future is to invent it.
-- Alan Kay