From: Rob Pitt Date: 2006-03-09T21:01:32+09:00 Subject: Re: Convert String to Hash *throws banana ad Bernhard* no no no don't eval user supplied strings you are asking to be hacked :) On 9 Mar 2006, at 11:58, Bernhard 'elven' Stoeckner wrote: > Nick Snels scribbled on Thursday 09 Mar 2006 12:52: >> Hi, >> >> I get a string inputted in my program, which I convert to a hash-like >> string, like: >> >> sql = '"table_alias" => [],"original_string" => "select * from >> table"' >> >> The string has the form of a hash, now I would like to convert this >> string to an actual hash. I tried it like: >> >> test = Hash[sql] >> or >> test = Hash.[](sql) >> >> but I get the following error: >> test2.rb:2:in `[]': odd number of arguments for Hash (ArgumentError) >> from test2.rb:2 >> >> When I do it like, >> >> test = Hash.new >> test = sql >> >> test becomes a String and is no longer a Hash. >> >> Is there an easy way to convert my hash-like string to an actual >> hash? >> Thanks for any help. >> >> Kind regards, >> >> Nick > > Hi Nick, > > no, you can't pass it to Hash.new directly, you'd have to write a > parser for > it yourself. If you're feeling lucky, you can use eval() to do that. > > *takes cover from flying bananas and such* > > Bernhard >