Is this a bug?

Discussion in 'Ruby' started by Vivek, Jan 12, 2006.

  1. Vivek

    Vivek Guest

    I have the below sample program.

    a=[1,2,3,1]
    b=[1,[2,4],[5,6,3],4]
    b.flatten!
    a.push b
    a.uniq!
    puts a

    The output it produces is
    1
    2
    3
    1
    2
    4
    5
    6
    3
    4

    a.uniq! doesnt seem to work on the array created after appending the
    flattened b array to a.
    Note that uniq! has actually purged the last element '1' in the
    original array a.It looks like
    uniq! somehow still works on the old array a and is not aware of the
    new array.
    I am using 1.8.4

    Vivek
     
    Vivek, Jan 12, 2006
    #1
    1. Advertising

  2. On Jan 11, 2006, at 8:53 PM, Vivek wrote:
    > I have the below sample program.
    >
    > a=[1,2,3,1]
    > b=[1,[2,4],[5,6,3],4]
    > b.flatten!
    > a.push b
    > a.uniq!
    > puts a


    rb(main):001:0> a=[1,2,3,1]
    => [1, 2, 3, 1]
    irb(main):002:0> b=[1,[2,4],[5,6,3],4]
    => [1, [2, 4], [5, 6, 3], 4]
    irb(main):003:0> b.flatten!
    => [1, 2, 4, 5, 6, 3, 4]
    irb(main):004:0> a.push b
    => [1, 2, 3, 1, [1, 2, 4, 5, 6, 3, 4]]
    irb(main):005:0> a.uniq!
    => [1, 2, 3, [1, 2, 4, 5, 6, 3, 4]]
    irb(main):006:0> puts a
    1
    2
    3
    1
    2
    4
    5
    6
    3
    4
    => nil

    Pushing b onto a does not push b's *values* onto a.
     
    Gavin Kistner, Jan 12, 2006
    #2
    1. Advertising

  3. Vivek

    Vivek Guest

    Ah! thanks for the clarification!
    Just had to one flatten after pushing b on a to acheive what I wanted.
     
    Vivek, Jan 12, 2006
    #3
  4. Vivek

    Kev Jackson Guest

    Gavin Kistner wrote:

    > On Jan 11, 2006, at 8:53 PM, Vivek wrote:
    >
    >> I have the below sample program.
    >>
    >> a=[1,2,3,1]
    >> b=[1,[2,4],[5,6,3],4]
    >> b.flatten!
    >> a.push b
    >> a.uniq!
    >> puts a

    >
    >
    > rb(main):001:0> a=[1,2,3,1]
    > => [1, 2, 3, 1]
    > irb(main):002:0> b=[1,[2,4],[5,6,3],4]
    > => [1, [2, 4], [5, 6, 3], 4]
    > irb(main):003:0> b.flatten!
    > => [1, 2, 4, 5, 6, 3, 4]
    > irb(main):004:0> a.push b
    > => [1, 2, 3, 1, [1, 2, 4, 5, 6, 3, 4]]
    > irb(main):005:0> a.uniq!
    > => [1, 2, 3, [1, 2, 4, 5, 6, 3, 4]]
    > irb(main):006:0> puts a
    > 1
    > 2
    > 3
    > 1
    > 2
    > 4
    > 5
    > 6
    > 3
    > 4
    > => nil
    >
    > Pushing b onto a does not push b's *values* onto a.
    >
    >

    ie you'd also need to flatten a after pushing on b

    a=[1,2,3,1]
    b=[1,[2,4],[5,6,3],4]
    b.flatten!
    a.push b
    a.flatten!
    a.uniq!
    puts a

    irb(main):001:0> a=[1,2,3,1]
    => [1, 2, 3, 1]
    irb(main):002:0> b=[1,[2,4],[5,6,3],4]
    => [1, [2, 4], [5, 6, 3], 4]
    irb(main):003:0> b.flatten!
    => [1, 2, 4, 5, 6, 3, 4]
    irb(main):004:0> a.push b
    => [1, 2, 3, 1, [1, 2, 4, 5, 6, 3, 4]]
    irb(main):005:0> a.flatten!
    => [1, 2, 3, 1, 1, 2, 4, 5, 6, 3, 4]
    irb(main):006:0> a.uniq!
    => [1, 2, 3, 4, 5, 6]
    irb(main):007:0> puts a
    1
    2
    3
    4
    5
    6
    => nil
    irb(main):008:0>
     
    Kev Jackson, Jan 12, 2006
    #4
  5. Vivek wrote:
    > Ah! thanks for the clarification!
    > Just had to one flatten after pushing b on a to acheive what I wanted.


    a=[1,2,3,1]
    b=[1,[2,4],[5,6,3],4]
    b.flatten!
    a.concat b # concat instead of push
    a.uniq!
    puts a

    Would also do what you wanted. It's probably faster than push and flatten.

    Robin
     
    Robin Stocker, Jan 12, 2006
    #5
    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.
Similar Threads
  1. John

    Re: BUG? OR NOT A BUG?

    John, Sep 20, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    555
  2. RedEye
    Replies:
    2
    Views:
    588
    Jason Kester
    Dec 13, 2005
  3. Michel Joly de Lotbiniere

    Bug Parade Bug 4953793

    Michel Joly de Lotbiniere, Nov 30, 2003, in forum: Java
    Replies:
    4
    Views:
    649
    Michel
    Dec 2, 2003
  4. DarkSpy
    Replies:
    4
    Views:
    893
    tom_usenet
    Jun 27, 2003
  5. Steve Holden
    Replies:
    1
    Views:
    402
    Behrang Dadsetan
    Jul 2, 2003
Loading...

Share This Page