I'd like to have my array behave like this: If I add an item to a[5],
a[0..5] will be equal to " " rather than nil. I tried to following:
irb(main):001:0> a=[" "]
=> [" "]
irb(main):002:0> a[5] = "test"
=> "test"
irb(main):003:0> a.inspect
=> "[\" \", nil, nil, nil, nil, \"test\"]"
Is there any way to have those nils default to " "?
Thanks,
Drew
···
--
Posted via http://www.ruby-forum.com/.
Array.new should do what you want:
a = Array.new(5, " ") # => [" ", " ", " ", " ", " "]
Dave
···
On 1/16/07, Drew Olson <olsonas@gmail.com> wrote:
I'd like to have my array behave like this: If I add an item to a[5],
a[0..5] will be equal to " " rather than nil. I tried to following:
irb(main):001:0> a=[" "]
=> [" "]
irb(main):002:0> a[5] = "test"
=> "test"
irb(main):003:0> a.inspect
=> "[\" \", nil, nil, nil, nil, \"test\"]"
Is there any way to have those nils default to " "?
--
Dave Goodlad
dgoodlad@gmail.com or dave@goodlad.ca
http://david.goodlad.ca/
If you know the size of the array, you could do:
a=Array.new(10, " ")
Or you could map the array afterwards:
a.map!{|e|e?' ':e}
···
On 1/16/07, Drew Olson <olsonas@gmail.com> wrote:
I'd like to have my array behave like this: If I add an item to a[5],
a[0..5] will be equal to " " rather than nil. I tried to following:
irb(main):001:0> a=[" "]
=> [" "]
irb(main):002:0> a[5] = "test"
=> "test"
irb(main):003:0> a.inspect
=> "[\" \", nil, nil, nil, nil, \"test\"]"
Is there any way to have those nils default to " "?
Thanks,
Drew
--
Posted via http://www.ruby-forum.com/\.
a=[" "]
x=5
(x-1).times do |x|
a=" "
end
It's a bit more code, but also more flexible, filling up everything up to
and not including 5.
···
On 1/16/07, David Goodlad <dgoodlad@gmail.com> wrote:
On 1/16/07, Drew Olson <olsonas@gmail.com> wrote:
> I'd like to have my array behave like this: If I add an item to a[5],
> a[0..5] will be equal to " " rather than nil. I tried to following:
>
> irb(main):001:0> a=[" "]
> => [" "]
> irb(main):002:0> a[5] = "test"
> => "test"
> irb(main):003:0> a.inspect
> => "[\" \", nil, nil, nil, nil, \"test\"]"
>
> Is there any way to have those nils default to " "?
You can also do:
Just noticed that a.map!{|e|e?' ':e} throws parse error on my machine, is
this the expected behavior?
Following statements work fine:
a.map!{|e|e||' '}
a.map!{|e|(e)?' ':e}
···
On 1/16/07, Chunyun Zhao <chunyun.zhao@gmail.com> wrote:
If you know the size of the array, you could do:
a=Array.new(10, " ")
Or you could map the array afterwards:
a.map!{|e|e?' ':e}
On 1/16/07, Drew Olson <olsonas@gmail.com> wrote:
>
> I'd like to have my array behave like this: If I add an item to a[5],
> a[0..5] will be equal to " " rather than nil. I tried to following:
>
> irb(main):001:0> a=[" "]
> => [" "]
> irb(main):002:0> a[5] = "test"
> => "test"
> irb(main):003:0> a.inspect
> => "[\" \", nil, nil, nil, nil, \"test\"]"
>
> Is there any way to have those nils default to " "?
>
> Thanks,
> Drew
>
> --
> Posted via http://www.ruby-forum.com/\.
>
Tamreen Khan wrote:
It's a bit more code, but also more flexible, filling up everything up
to
and not including 5.
I should have specified the follow:
1. I'm golfing, so short length would be preferred.
2. I will not know the length of the array beforehand.
Thanks again
-Drew
···
--
Posted via http://www.ruby-forum.com/\.