# How to convert a string of arbitrary size to an array

**URL:** <https://www.ruby-forum.com/t/how-to-convert-a-string-of-arbitrary-size-to-an-array/159884>\
**Category:** Ruby\
**Created:** [February 9, 2009, 6:44pm UTC](https://www.ruby-forum.com/t/how-to-convert-a-string-of-arbitrary-size-to-an-array/159884 "2009-02-09T18:44:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@rubina](https://www.ruby-forum.com/u/rubina)\
**Post date:** [February 9, 2009, 6:44pm UTC](https://www.ruby-forum.com/t/how-to-convert-a-string-of-arbitrary-size-to-an-array/159884/1 "2009-02-09T18:44:45Z")

</div>

Team,

I am trying to convert a string of arbitrary size to an array of single  
characters, bu I having a hard time.  
A google search “Ruby” “string to array” returned thousands of hits. I  
checked couple dozens but none satisfy what I want.

Say I get a user-supplied string (alphanumeric & special chars) of  
arbitrary  
length.  
How can I convert that string to an array of single chars?

Thank you

---

<div class="post-metadata">

**Author:** ![rubina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@rubina](https://www.ruby-forum.com/u/rubina)\
**Post date:** [February 9, 2009, 6:47pm UTC](https://www.ruby-forum.com/t/how-to-convert-a-string-of-arbitrary-size-to-an-array/159884/2 "2009-02-09T18:47:41Z")

</div>

On Mon, Feb 9, 2009 at 9:43 AM, Ruby S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I am trying to convert a string of arbitrary size to an array of single  
> characters, bu I having a hard time.

Check out String#split:

“abcdefg”.split( // )  
=\> [“a”, “b”, “c”, “d”, “e”, “f”, “g”]

Ben

---

<div class="post-metadata">

**Author:** ![rubina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@rubina](https://www.ruby-forum.com/u/rubina)\
**Post date:** [February 9, 2009, 6:58pm UTC](https://www.ruby-forum.com/t/how-to-convert-a-string-of-arbitrary-size-to-an-array/159884/3 "2009-02-09T18:58:25Z")

</div>

In Ruby1.9 I am quite fond of this  
514/16 \> ruby19 -ve ‘p “Hello World!”.each\_char.to\_a’  
ruby 1.9.1p0 (2009-01-30 revision 21907) [i686-linux]  
[“H”, “e”, “l”, “l”, “o”, " ", “W”, “o”, “r”, “l”, “d”, “!”]

R

---

<div class="post-metadata">

**Author:** ![rubina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@rubina](https://www.ruby-forum.com/u/rubina)\
**Post date:** [February 9, 2009, 7:07pm UTC](https://www.ruby-forum.com/t/how-to-convert-a-string-of-arbitrary-size-to-an-array/159884/4 "2009-02-09T19:07:16Z")

</div>

On Mon, Feb 9, 2009 at 12:45 PM, Ben B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Ben

Aha!  
I tried:  
“abcdefg”.split( / / ) # Space between //  
“abcdefg”.scan(//)  
and other…

Thank you very much for your quick reply.

Ruby S.

---

<div class="post-metadata">

**Author:** ![rubina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@rubina](https://www.ruby-forum.com/u/rubina)\
**Post date:** [February 9, 2009, 7:06pm UTC](https://www.ruby-forum.com/t/how-to-convert-a-string-of-arbitrary-size-to-an-array/159884/5 "2009-02-09T19:06:06Z")

</div>

On Mon, Feb 9, 2009 at 12:57 PM, Robert D.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> longer without taking into account not only the world as it is, but  
> the world as it will be … ~ Isaac Asimov

Thank you Robert, it is truly appreciated!
