# Array question

**URL:** <https://www.ruby-forum.com/t/array-question/177944>\
**Category:** Ruby\
**Created:** [October 27, 2009, 1:38am UTC](https://www.ruby-forum.com/t/array-question/177944 "2009-10-27T01:38:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubymarc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9bcb4/32.png) [@rubymarc](https://www.ruby-forum.com/u/rubymarc)\
**Post date:** [October 27, 2009, 1:38am UTC](https://www.ruby-forum.com/t/array-question/177944/1 "2009-10-27T01:38:28Z")

</div>

Hey everyone I’m a 14 year-old beginner in ruby and I discovered that I  
need to separate strings into arrays for a program that I am making.  
For example, if I have the string ‘abc’, I’ll want to make it into the  
array [‘a’, ‘b’, ‘c’]. Is there any way to do this? I am using version  
1.8.6, by the way. All help is welcome.

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![rubymarc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9bcb4/32.png) [@rubymarc](https://www.ruby-forum.com/u/rubymarc)\
**Post date:** [October 27, 2009, 1:47am UTC](https://www.ruby-forum.com/t/array-question/177944/2 "2009-10-27T01:47:30Z")

</div>

Hi –

On Tue, 27 Oct 2009, Ruby Mokx wrote:

> Hey everyone I’m a 14 year-old beginner in ruby and I discovered that I

Welcome!

> need to separate strings into arrays for a program that I am making.  
> For example, if I have the string ‘abc’, I’ll want to make it into the  
> array [‘a’, ‘b’, ‘c’]. Is there any way to do this? I am using version  
> 1.8.6, by the way. All help is welcome.

The method you want is String#split:

‘abc’.split(//) # =\> [‘a’, ‘b’, ‘c’]

Split takes a regular expression or string as its argument, and splits  
on that pattern. The empty regular expression causes it to split the  
string into individual characters.

There are some more bells and whistles to it but that’s the basic  
idea.

David

–  
The Ruby training with D. Black, G. Brown, J.McAnally  
Compleat Jan 22-23, 2010, Tampa, FL  
Rubyist [http://www.thecompleatrubyist.com](http://www.thecompleatrubyist.com)

David A. Black/Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![rubymarc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9bcb4/32.png) [@rubymarc](https://www.ruby-forum.com/u/rubymarc)\
**Post date:** [October 27, 2009, 4:04am UTC](https://www.ruby-forum.com/t/array-question/177944/3 "2009-10-27T04:04:29Z")

</div>

On Oct 26, 6:46 pm, “David A. Black” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > need to separate strings into arrays for a program that I am making.  
> > For example, if I have the string ‘abc’, I’ll want to make it into the  
> > array [‘a’, ‘b’, ‘c’]. Is there any way to do this? I am using version  
> > 1.8.6, by the way. All help is welcome.
> 
> The method you want is String#split:
> 
> ‘abc’.split(//) # =\> [‘a’, ‘b’, ‘c’]

Here are two other techniques, just for fun:

all\_characters = “abc”.scan(/./)

all\_characters=[]  
“abc”.each\_char{ |x| all\_characters.push(x) }

---

<div class="post-metadata">

**Author:** ![rubymarc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9bcb4/32.png) [@rubymarc](https://www.ruby-forum.com/u/rubymarc)\
**Post date:** [October 27, 2009, 5:44am UTC](https://www.ruby-forum.com/t/array-question/177944/4 "2009-10-27T05:44:34Z")

</div>

Hi,

Am Dienstag, 27. Okt 2009, 09:38:30 +0900 schrieb Ruby Mokx:

> Hey everyone I’m a 14 year-old beginner in ruby and I discovered that I  
> need to separate strings into arrays for a program that I am making.  
> For example, if I have the string ‘abc’, I’ll want to make it into the  
> array [‘a’, ‘b’, ‘c’]. Is there any way to do this? I am using version  
> 1.8.6, by the way. All help is welcome.

Hey Mokx, the answer has been given. Let me start to disturb you.  
There’s a difference between

str.scan /./ # and  
str.scan /./u

Your enthusiasm ist delighting me but the real world is rough.  
Cheer up.

Bertram

---

<div class="post-metadata">

**Author:** ![rubymarc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9bcb4/32.png) [@rubymarc](https://www.ruby-forum.com/u/rubymarc)\
**Post date:** [October 27, 2009, 12:01pm UTC](https://www.ruby-forum.com/t/array-question/177944/5 "2009-10-27T12:01:32Z")

</div>

Hi –

On Tue, 27 Oct 2009, Phrogz wrote:

> Here are two other techniques, just for fun:
> 
> all\_characters = “abc”.scan(/./)

That won’t include newline characters, though, unless you use the /m  
modifier.

> all\_characters=[]  
> “abc”.each\_char{ |x| all\_characters.push(x) }

🙂 Actually in 1.9 you could do:

str.chars.to\_a

but he’s using 1.8.6.

David

–  
The Ruby training with D. Black, G. Brown, J.McAnally  
Compleat Jan 22-23, 2010, Tampa, FL  
Rubyist [http://www.thecompleatrubyist.com](http://www.thecompleatrubyist.com)

David A. Black/Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))
