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?
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
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
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 Law wrote in post #1110051:
OK, I thought it was worth checking. Often people do not ask the question that they should.
Colin
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