(noob) cast string to array?

(David Naseby) #1

Or easier, avoiding eval again:

u = s.scan( /[\w\s]+/ )



-----Original Message-----
From: “Peña, Botp” [mailto:botp@delmonte-phil.com]

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: