9

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.

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

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

2
  • 1
    I would store it as JSON, read the file and use JSON.parse Commented Nov 12, 2016 at 20:09
  • 2
    use YML (yaml) or JSON to store hash in file and read it in ruby file as its actual data structure. Commented Nov 12, 2016 at 20:10

4 Answers 4

13

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

Sign up to request clarification or add additional context in comments.

Comments

4

You can try YAML.load method

Example:

 YAML.load("{test: 't_value'}")

This will return following hash.

 {"test"=>"t_value"}

You can also use eval method

Example:

 eval("{test: 't_value'}")

This will also return same hash

  {"test"=>"t_value"} 

Hope this will help.

Comments

2

I would to this using the json gem.

In your Gemfile you use

gem 'json'

and then run bundle install.

In your program you require the gem.

require 'json'

And then you may create your "Hashfield" string by doing:

hash_as_string = hash_object.to_json

and write this to your flat file.

Finally, you may read it easily by doing:

my_hash = JSON.load(File.read('your_flat_file_name'))

This is simple and very easy to do.

Comments

0

Should it not be clear, it is only the hash that must be contained in a JSON file. Suppose that file is "simpleexample.json":

puts File.read("simpleexample.json")
  # #{"test1":{"url":"http://www.google.com"},"test2":{"{:title=>\"This\"}":{"failure":"sendemal"}}}

The code can be in a normal Ruby source file, "simpleclass.rb":

puts File.read("simpleclass.rb")
  # class Simple
  #   def initialize(example_file_name)
  #     @data = JSON.parse(File.read(example_file_name))
  #   end
  #   def print
  #     @data
  #   end
  # end

Then we can write:

require 'json'
require_relative "simpleclass"

a = Simple.new("simpleexample.json")
  #=> #<Simple:0x007ffd2189bab8 @data={"test1"=>{"url"=>"http://www.google.com"},
  #     "test2"=>{"{:title=>\"This\"}"=>{"failure"=>"sendemal"}}}> 
a.print
  #=> {"test1"=>{"url"=>"http://www.google.com"},
  #    "test2"=>{"{:title=>\"This\"}"=>{"failure"=>"sendemal"}}} 
a.class
  #=> Simple 

To construct the JSON file from the hash:

h = { :test1=>{ :url=>'http://www.google.com' },
      :test2=>{ { :title=>'This' }=>{:failure=>'sendemal' } } }

we write:

File.write("simpleexample.json", JSON.generate(h))
  #=> 95 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.