From: David Naseby Date: 2004-02-12T11:55:26+09:00 Subject: Re: (noob) cast string to array? >-----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" ;-) > >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 :-) > Or easier, avoiding eval again: u = s.scan( /[\w\s]+/ ) David