# Array trouble

**URL:** <https://www.ruby-forum.com/t/array-trouble/72878>\
**Category:** Ruby\
**Created:** [October 19, 2006, 4:59pm UTC](https://www.ruby-forum.com/t/array-trouble/72878 "2006-10-19T16:59:26Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![gijs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/9fc348/32.png) [@gijs](https://www.ruby-forum.com/u/gijs)\
**Post date:** [October 19, 2006, 4:59pm UTC](https://www.ruby-forum.com/t/array-trouble/72878/1 "2006-10-19T16:59:26Z")

</div>

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?  
thanks

gijs

---

<div class="post-metadata">

**Author:** ![gijs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/9fc348/32.png) [@gijs](https://www.ruby-forum.com/u/gijs)\
**Post date:** [October 19, 2006, 5:06pm UTC](https://www.ruby-forum.com/t/array-trouble/72878/2 "2006-10-19T17:06:14Z")

</div>

On 19.10.2006 16:59, Gijs N. wrote:

> 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
```

---

<div class="post-metadata">

**Author:** ![gijs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/9fc348/32.png) [@gijs](https://www.ruby-forum.com/u/gijs)\
**Post date:** [October 19, 2006, 5:10pm UTC](https://www.ruby-forum.com/t/array-trouble/72878/3 "2006-10-19T17:10:36Z")

</div>

Gijs N. wrote:

> but I expected it to give "a b c d "  
> How about

“abcd”.split(‘’).join(’ ')

Peter  
[http://www.rubyrailways.com](http://www.rubyrailways.com)

---

<div class="post-metadata">

**Author:** ![gijs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/9fc348/32.png) [@gijs](https://www.ruby-forum.com/u/gijs)\
**Post date:** [October 19, 2006, 5:24pm UTC](https://www.ruby-forum.com/t/array-trouble/72878/4 "2006-10-19T17:24:40Z")

</div>

This should work also:

```
'abcd'.scan(/./).join(' ')
```

---

<div class="post-metadata">

**Author:** ![gijs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/9fc348/32.png) [@gijs](https://www.ruby-forum.com/u/gijs)\
**Post date:** [October 19, 2006, 5:22pm UTC](https://www.ruby-forum.com/t/array-trouble/72878/5 "2006-10-19T17:22:39Z")

</div>

Also:

“abcd”.split("").map do |w|  
w + " "  
end.join

would give you the extra space at the end.

---

<div class="post-metadata">

**Author:** ![gijs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/9fc348/32.png) [@gijs](https://www.ruby-forum.com/u/gijs)\
**Post date:** [October 19, 2006, 5:30pm UTC](https://www.ruby-forum.com/t/array-trouble/72878/6 "2006-10-19T17:30:52Z")

</div>

Louis J Scoras wrote:

> This should work also:
> 
> ‘abcd’.scan(/./).join(’ ')

Yeah, but it has the same flaw as mine (as pointed out by Tom):  
"a b c d " != “a b c d”

but

‘abcd’.scan(/./).join(’ ') + " "

resp

“abcd”.split(‘’).join(’ ') + " "

should solve also this problem…

Peter  
[http://www.rubyrailways.com](http://www.rubyrailways.com)

---

<div class="post-metadata">

**Author:** ![gijs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/9fc348/32.png) [@gijs](https://www.ruby-forum.com/u/gijs)\
**Post date:** [October 20, 2006, 7:56pm UTC](https://www.ruby-forum.com/t/array-trouble/72878/7 "2006-10-20T19:56:52Z")

</div>

On 10/19/06, Gijs N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> but I expected it to give "a b c d "
> 
> why doesnt this work as such?  
> thanks

Others have already given y

ou ways to do what you want to do. But I don’t think anyone answered  
your question about why what you did doesn’t work.

Let’s pick it apart. First of all it doesn’t really return “abcd” it  
returns [“abcd”] which is a array with a single element:

irb(main):001:0\> ret = " "  
irb(main):002:0\> “abcd”.to\_a.each do |w| ret += " "; end  
=\> [“abcd”]

So why is this?

First of all, the value of Array#each is the array itself:

irb(main):003:0\> [1, 2].each {|el| puts el }  
1  
2  
=\> [1, 2]

And, as Robert pointed out, “abcd”.to\_a doesn’t do what you expect, it  
returns an array of lines in the string, and there’s only one line in  
“abcd” so:

irb(main):004:0\> “abcd”.to\_a  
=\> [“abcd”]

Now your code looks like it’s trying to accumulate your result in ret,  
lets’ run it again, but we’ll postpone the problem with String.to\_a by  
breaking up the array into characters, “by hand.”

irb(main):005:0\* ret = " "  
irb(main):006:0\> [“a”, “b”, “c”, “d”].each { |w| ret += " " }  
=\> [“a”, “b”, “c”, “d”]  
irb(main):007:0\> ret  
=\> " "

Oops, we weren’t actually adding the elements, so ret just ended up as  
a string with as many blanks as were characters. So let’s fix that:

irb(main):008:0\> ret = " "  
rb(main):009:0\> [“a”, “b”, “c”, “d”].each { |w| ret += w + " " }  
=\> [“a”, “b”, “c”, “d”]  
irb(main):010:0\> ret  
=\> " a b c d "

Okay, that looks better.

Now others have given one way to break up the string into an array of  
chars:

irb(main):011:0\> “abcd”.split(//)  
=\> [“a”, “b”, “c”, “d”]

let’s put this solution of the to\_a problem with what we just did:

irb(main):012:0\>ret = ‘’  
rb(main):013:0\> “abcd”.split(//).each { |w| ret += w + " " }  
=\> [“a”, “b”, “c”, “d”]  
irb(main):014:0\> ret  
=\> " a b c d "

So this works, as do the others. One more way to do this which is  
related to the approach we’ve been taking is to use inject.

irb(main):018:0\> “abcd”.split(//).inject(“”) { |result, char| result  
+= char + " " }  
=\> "a b c d "

This avoids having to use the auxiliary variable ret, since the result  
of inject is the value accumulated in the first argument to it’s block  
argument.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![gijs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/9fc348/32.png) [@gijs](https://www.ruby-forum.com/u/gijs)\
**Post date:** [October 20, 2006, 8:40pm UTC](https://www.ruby-forum.com/t/array-trouble/72878/8 "2006-10-20T20:40:18Z")

</div>

Thanks for all the replies, I really learned some important basics here.

Rick Denatale wrote:

> On 10/19/06, Gijs N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > but I expected it to give "a b c d "  
> > …  
> > –  
> > Rick DeNatale
> 
> My blog on Ruby  
> [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)
