Kev Jackson
1/12/2006 4:05:00 AM
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>