(noob) cast string to array?


(David Naseby) #1

From: Koncept [mailto:user@unknown.invalid]
In article 200402120054.23296.martin@zsdfherg.com, Martin Hart

irb(main):002:0> s = ‘[“a string”, “another string”]’
=> "[“a string”, “another string”]"
irb(main):003:0> array = eval s
=> [“a string”, “another string”]
irb(main):004:0> array.class
=> Array

This is great! Thanks Martin. Thanks for all the fast responses as well
everybody.

Avoid eval!!!

I just wanted to follow up on some of the responses here:

  1. Suggestion to use gsub over sub. I assume that gsub is global?

You assume correctly. Either will work in your particular case.

  1. what is the “!” responsible for doing? (Remember me = n00b )

Its a hint for a “dangerous” method - generally, one that changes the
receiver. It signals to the reader its intent, but syntactically, its just
part of the identifier. Its like ? methods should return a bool (eg,
x.is_nil? ).

David
http://homepages.ihug.com.au/~naseby/

···

-----Original Message-----
martin@zsdfherg.com wrote:


(Martin Hart) #2

I think that may be a bit harsh :slight_smile:
As long as one is aware of when and when not to use it (the same as
everything else) then it has a useful and valid purpose.

Cheers,
Martin

···

On Thursday 12 February 2004 01:34, David Naseby wrote:

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

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

In article 200402120054.23296.martin@zsdfherg.com, Martin Hart

martin@zsdfherg.com wrote:

irb(main):002:0> s = ‘[“a string”, “another string”]’
=> "[“a string”, “another string”]"
irb(main):003:0> array = eval s
=> [“a string”, “another string”]
irb(main):004:0> array.class
=> Array

This is great! Thanks Martin. Thanks for all the fast responses as well
everybody.

Avoid eval!!!