How to convert a string of arbitrary size to an array

R

Ruby Student

[Note: parts of this message were removed to make it a legal post.]

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
 
B

Ben Bleything

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
 
R

Robert Dober

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
 
R

Ruby Student

[Note: parts of this message were removed to make it a legal post.]

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

--
It is change, continuing change, inevitable change, that is the
dominant factor in society today. No sensible decision can be made any
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!
 
R

Ruby Student

[Note: parts of this message were removed to make it a legal post.]

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

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

Thank you very much for your quick reply.


Ruby Student
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top