From: Bug Free on 1 Jun 2010 13:32 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 1 Jun 2010 15:45 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 1 Jun 2010 21:16 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 2 Jun 2010 02:36 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
First
|
Prev
|
Pages: 1 2 3 4 Prev: Graphical programming with Qt Next: Does Rails 3.0.0 beta3 work with ruby 1.9.1 on Windows XP? |