Forum: Ruby How to convert a string of arbitrary size to an array

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ruby S. (Guest)
on 2009-02-09 19:44
(Received via mailing list)
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
Ben B. (Guest)
on 2009-02-09 19:47
(Received via mailing list)
On Mon, Feb 9, 2009 at 9:43 AM, Ruby S. <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
Robert D. (Guest)
on 2009-02-09 19:58
(Received via mailing list)
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
Ruby S. (Guest)
on 2009-02-09 20:06
(Received via mailing list)
On Mon, Feb 9, 2009 at 12:57 PM, Robert D.
<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!
Ruby S. (Guest)
on 2009-02-09 20:07
(Received via mailing list)
On Mon, Feb 9, 2009 at 12:45 PM, Ben B. <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.
This topic is locked and can not be replied to.