each_with_what_index?

T

T. Onoma

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
 
G

Gavin Sinclair

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
 
T

T. Onoma

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.
 
R

Richard Kilmer

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.
 
H

Hugh Sasse Staff Elec Eng

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
 
T

T. Onoma

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.
 
N

nobu.nokada

Hi,

At Thu, 4 Dec 2003 10:42:07 +0900,
Mark said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top