user3063045 user3063045 - 1 year ago 214
Ruby Question

Ruby Convert String to Hash

I'm storing configuration data in hashes written in flat files. I want to import the hashes into my Class so that I can invoke corresponding methods.


{ :test1 => { :url => '' }, :test2 => {{ :title => 'This' } => {:failure => 'sendemal'}}}


class Simple
def initialize(file_name)
# Parse the hash
file =, "r")
@data =

def print

a ="simpleexample.rb")
b = a.print
puts b.class # => String

How do I convert any "Hashified" String into an actual Hash?

Answer Source

You can use eval(@data), but really it would be better to use a safer and simpler data format like JSON.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download