(noob) cast string to array?


("Peña, Botp") #1

Koncept [mailto:user@unknown.invalid] humbly inquired:

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 think you do not need “[]” replacement since you just need an “array”.
Remember, it just “looks like” :wink:

c:\family\ruby>ruby -w a1.rb
u is of class Array
0 'this’
1 'is a’
2 ‘test of wits’

c:\family\ruby>cat a1.rb
s = “{‘this’,‘is a’,‘test of wits’}” # just a string
t = s.gsub(/[{}]/,’’) # no more {}
u = t.split(’,’).to_a # convert to array
puts "u is of class #{u.class}"
u.each_index {|i| puts “#{i} #{u[i]}”}

I used each_index just in case you’ll doubt if it’s really an array. note,
no eval there :slight_smile:

Koncept <<

kind regards -botp