(noob) cast string to array?


(David Naseby) #1

From: Koncept [mailto:user@unknown.invalid]

Hi. What a fantastic language. This is my first day learning using it
and I love it so far.

Welcome to the language. The vast majority of us love it too.

result = { “foobar”, “foobar”, “foobar” }

I want to basically remove the {}'s and replace them with [] and cast
result as an array so that I can have some fun within Ruby.

Don’t think casting, as its an unhealthy thought. The most direct, but least
recommended method for achieving your goal is evil eval:

result = '{ “foobar”, “foobar”, “foobar” }'
result.gsub!( /{/, ‘[’ )
result.gsub!( /}/, ‘]’ )
arr = eval( result )

It works. Don’t do it.

Better to parse the input. The simplest possible way is:

arr = result.scan( /w+/ )

If the strings can only be single words within the quotes.
David Naseby
http://homepages.ihug.com.au/~naseby/

···

-----Original Message-----