Robert Klemme
10/19/2006 3:02:00 PM
On 19.10.2006 16:59, Gijs Nijholt wrote:
> Hi
>
> I want to add a value to an arrays' values like this:
>
> ret = ""
> "abcd".to_a.each do |w|
> ret += " "
> end
>
> this gives => "abcd" in IRB
> but I expected it to give "a b c d "
>
> why doesnt this work as such?
First, String#to_a does not what you probably expect:
irb(main):001:0> "abcd".to_a
=> ["abcd"]
Here are workable solutions:
irb(main):002:0> "abcd".gsub(/./, '\\& ')
=> "a b c d "
irb(main):003:0> "abcd".split(//).map {|s| s + " "}.join
=> "a b c d "
Kind regards
robert