From: mike@... (Mike Stok) Date: 2004-02-12T12:59:59+09:00 Subject: Re: (noob) cast string to array? In article <110220041931304813%user@unknown.invalid>, Koncept wrote: >Hi. What a fantastic language. This is my first day learning using it >and I *love* it so far. > >I am not very comfortable with Ruby speak yet, so I hope the >explaination of my problem makes some kind of sense. > >I have a program that fetches some results from a shell script. The >result is a string, not a block as it may look like below. > >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. > >I figured something like this: > >result = result.sub(/\{/,'[') >result = result.sub(/\}/,']') > >Now I have a string that *LOOKS* like and Array but isn't.... One thing that you might consider is that you have a line which looks like a CSV data record with { } adound it, so you could use a library from the Ruby Application Archive to mangle it after you've trimmed the { and }. Alternatively, if the data is "well behaved", you can use a regular expression to pick it apart. Using irb (interactive ruby to experiment: >> result = '{ "robert", "trey", "adrian", "pat" }' => "{ \"robert\", \"trey\", \"adrian\", \"pat\" }" >> result.scan(/"(.*?)"/).flatten => ["robert", "trey", "adrian", "pat"] If you're familiar with regular expressions then they are well integrated into Ruby and can be powerful tools. If you're not then there have been plenty of other suggestions which may fit your needs. Hope this helps, Mike -- mike@stok.co.uk | The "`Stok' disclaimers" apply. http://www.stok.co.uk/~mike/ | GPG PGP Key 1024D/059913DA mike@exegenix.com | Fingerprint 0570 71CD 6790 7C28 3D60 http://www.exegenix.com/ | 75D2 9EC4 C1C0 0599 13DA