플랫 파일로 작성된 해시에 구성 데이터를 저장합니다. 나는 해시를 클래스에 가져 와서 해당 메서드를 호출 할 수 있습니다.Ruby 해시 변환 문자열
example.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
어떻게 실제 해시에있는 "Hashified"문자열로 변환합니까?
파일을 읽고 JSON.parse를 사용하십시오. – 23tux
YML (yaml) 또는 JSON을 사용하여 파일에 해시를 저장하고 실제 데이터 구조로 루비 파일에서 읽을 수 있습니다. – Sivalingam