Mage wrote: > Hello, > > I have a string like: ' "gaz"=>"1", "viz"=>"1", "lift"=>"0", > "kamra"=>"1", "klima"=>"0" ' > Is it possible to convert this easily to hash without calling eval or > writing regexp? > > eval is easy and unsecure. Regexp is not very simple if you want handle > every special case (nested values etc). > > Mage > You could do something like this: #!/usr/bin/env ruby # UziMonkey <uzimonkey / gmail.com> str = %q{"gaz"=>"1", "viz"=>"1", "lift"=>"0", "kamra"=>"1","klima"=>"0"} hash = Hash[* str. split(/, +/). map {|s| s.match( /"([^"]+)"=>"([^"]+)"/ )[1,2] }. flatten ] puts hash.inspect But the regex makes a lot of assumptions. If the format of the hash changes at all (for example, quotes removed or changed to single quotes), it will break. If not, it should work fine. -- Michael Morin Guide to Ruby http://ruby.about.com/ Become an About.com Guide: beaguide.about.com About.com is part of the New York Times Company