Can you see the bug'a'boo in the following snippet? a[1..-1].each_with_index do |t,i| puts "#{t} => #{a}" # ... end Now, I understand why it happens, but nonethess I spent a good while tracking it down (in a slightly more complex version). I guess that's just the way it has to be, but it is quite unintuitive. -t0

On Wednesday, December 3, 2003, 10:01:38 PM, T. wrote: > Can you see the bug'a'boo in the following snippet? > a[1..-1].each_with_index do |t,i| > puts "#{t} => #{a}" > # ... > end > Now, I understand why it happens, but nonethess I spent a good while tracking > it down (in a slightly more complex version). I guess that's just the way it > has to be, but it is quite unintuitive. Bugs can be like that sometimes, but there's absolutely nothing unintutive about that, unless you want Ruby to start guessing what you really mean all the time Maybe this is better for you? a.each_with_index do |t,i| next if i == 0 puts "#{t} => #{a}" # ... end Gavin

On Wednesday 03 December 2003 12:23 pm, Gavin Sinclair wrote: > > Bugs can be like that sometimes, but there's absolutely nothing > unintutive about that, unless you want Ruby to start guessing what you > really mean all the time Saying that there's "absolutely nothing unintutive about that" is a real stretch. Obviously, I have asked for certain indexes [1..-1], but we know that #[ ] is really just a form of slice, so what we get is a new array, thus reindexed form [0..a.length-2]. It's understandable, but I sure wouldn't call it intuitive. > Maybe this is better for you? > > a.each_with_index do |t,i| > next if i == 0 > puts "#{t} => #{a}" > # ... > end I did: a[1..-1].each_with_index do |t,i| puts "#{t} => #{a[i+1]}" # ... end Thinking it would be more efficient. Hmm...Or does this have a bug? T.

On Dec 3, 2003, at 9:50 AM, T. Onoma wrote: > Saying that there's "absolutely nothing unintutive about that" is a > real > stretch. Obviously, I have asked for certain indexes [1..-1], but we > know > that #[ ] is really just a form of slice, so what we get is a new > array, thus > reindexed form [0..a.length-2]. It's understandable, but I sure > wouldn't call > it intuitive. What you did was this: a = [1, 2, 3] b = a[1..-1] b.each_with_index do |t, i| puts "#{t} => #{a}" end You are generating a new array that is a subset of an existing array and then iterating over the new array with the new array's indexes. You then use that index to reference into the original array. Obviously the values would be different.

On Wed, 3 Dec 2003, T. Onoma wrote: > > Saying that there's "absolutely nothing unintutive about that" is a real > stretch. Obviously, I have asked for certain indexes [1..-1], but we know > that #[ ] is really just a form of slice, so what we get is a new array, thus > reindexed form [0..a.length-2]. It's understandable, but I sure wouldn't call > it intuitive. Suppose if each_with_index allowed a range parameter a.each_with_index(1...a.size) {|t,i|...} Would that be worth an RCR? It looks sort of natural to me, but... Hugh

On Wednesday 03 December 2003 04:13 pm, Hugh Sasse Staff Elec Eng wrote: > On Wed, 3 Dec 2003, T. Onoma wrote: > > Saying that there's "absolutely nothing unintutive about that" is a real > > stretch. Obviously, I have asked for certain indexes [1..-1], but we know > > that #[ ] is really just a form of slice, so what we get is a new array, > > thus reindexed form [0..a.length-2]. It's understandable, but I sure > > wouldn't call it intuitive. > > Suppose if each_with_index allowed a range parameter > a.each_with_index(1...a.size) {|t,i|...} > Would that be worth an RCR? It looks sort of natural to me, but... But? Seems good to me too, as far as I know each_with_index doesn't currently take any parameters. T.

Hi, At Thu, 4 Dec 2003 10:42:07 +0900, Mark J. Reed wrote: > You can fix that like this: > > module Enumerable > def each_with_index(range=(0...size)) > b, e, x = range.begin,range.end,range.exclude_end? > if b < 0 || e < 0 > b += size if b < 0 > e += size if e < 0 > range = Range.new(b,e,x) > end > range.each {|i| yield at(i),i} > end > end Fundamentally, these work only with Array, not Enumerable. -- Nobu Nakada