each_with_what_index?

Discussion in 'Ruby' started by T. Onoma, Dec 3, 2003.

  1. T. Onoma

    T. Onoma Guest

    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
     
    T. Onoma, Dec 3, 2003
    #1
    1. Advertising

  2. 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
     
    Gavin Sinclair, Dec 3, 2003
    #2
    1. Advertising

  3. T. Onoma

    T. Onoma Guest

    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.
     
    T. Onoma, Dec 3, 2003
    #3
  4. 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.
     
    Richard Kilmer, Dec 3, 2003
    #4
  5. 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
     
    Hugh Sasse Staff Elec Eng, Dec 3, 2003
    #5
  6. T. Onoma

    T. Onoma Guest

    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.
     
    T. Onoma, Dec 3, 2003
    #6
  7. T. Onoma

    Guest

    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
     
    , Dec 4, 2003
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page