我将配置数据存储在平面文件中编写的哈希中。我想将这些哈希导入到我的类中,以便我可以调用相应的方法。
例子.rb
例子.rb
{
:test1 => { :url => 'http://www.google.com' },
:test2 => {
{ :title => 'This' } => {:failure => 'sendemal'}
}
}
simpleclass.rb
class Simple
def initialize(file_name)
# Parse the hash
file = File.open(file_name, "r")
@data = file.read
file.close
end
def print
@data
end
a = Simple.new("simpleexample.rb")
b = a.print
puts b.class # => String
我如何将任何“哈希化”字符串转换为实际哈希?
JSON.parse
函数。 - 23tux