How can I initialize an array and be able to add nil value to it? as I know Array.wrap doesn't do the job.
list = Array.wrap(nil) => []
What I want:
list = Array.add(nil) => [nil]
Thank you
Try:
list = Array.new(1)
The number fed in as an argument dictates how many nils are added:
list = Array.new(3)
=> [nil, nil, nil]
[item]How can I initialize an array and be able to add nil value to it?item can also be an Array I would change this to [*item] to better simulate the way Array#wrap works (which by the way is a rails method so I added the rails tag.nil or arrays or non-arrays might indicate some underlying problems.Maybe you are looking for (Rails):
list = Array.wrap([nil])
#=> [nil]
But why not just a simple list = [nil], as per @engineersmnky's comment?
Also list = Array.new.push nil, but it's still better the easy way above.
[nil]nilwith the given object if it is something other thannil. If the object is held by a variablex, write[x].