Good Ruubyist way

a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
b = a + [nil]
p a #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
p b #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, nil]

Is there any other Rubyist way to get the b array from a array?

b = a.push nil

Walter

Oh, duh, never mind, that pushes it onto a also.

Walter

Okay, this works:

1.9.3-p392 :009 > a
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

1.9.3-p392 :010 > b = [a, nil].flatten
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, nil]

1.9.3-p392 :011 > b
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, nil]
1.9.3-p392 :012 > a
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Colin L. wrote in post #1110051:

On 24 May 2013 14:56, Love U Ruby [email protected] wrote:

a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
b = a + [nil]
p a #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
p b #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, nil]

Is there any other Rubyist way to get the b array from a array?

If what you really want is to add a nil element to a then you can just
do
a << nil
or, as Walter suggested
a.push nil

Colin

@Colin - I don’t want to modify the array a. Thus push and <<
can’t be helpful. :slight_smile:

On 24 May 2013 16:20, Love U Ruby [email protected] wrote:

do
a << nil
or, as Walter suggested
a.push nil

Colin

@Colin - I don’t want to modify the array a. Thus push and <<
can’t be helpful. :slight_smile:

OK, I thought it was worth checking. Often people do not ask the
question that they should.

Colin

On 24 May 2013 14:56, Love U Ruby [email protected] wrote:

a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
b = a + [nil]
p a #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
p b #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, nil]

Is there any other Rubyist way to get the b array from a array?

If what you really want is to add a nil element to a then you can just
do
a << nil
or, as Walter suggested
a.push nil

Colin

On Fri, May 24, 2013 at 10:33 AM, Paul J. [email protected] wrote:

a = [1,2,3,4,5,6,7,8,9,10]
b= a.reverse.reverse

Can’t tell if you are trolling or if you think that’s actually a good
idea. How about just doing:

a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
b = a.dup

a = [1,2,3,4,5,6,7,8,9,10]
b= a.reverse.reverse