Ruby: How to load a hash from a file?

Is there a Ruby tool that would allow me to load an abbreviation file in the format (Shortened => Abr)? Then I need to read each word from another file. If the word matches the word in abbreviations, I need to change it to an abbreviated word. I think I can use Hash, but I don’t know how to load it from a file.

+6
source share
3 answers

YAML is a very universal format for storing data in a way that can be transferred between applications and programming languages. JSON is another option that is very common on websites.

I use YAML for things like configuration files because it is very easy to read and modify. For example, this Ruby structure:

irb(main):002:0> foo = { 'a' => 1, 'b' => [2, 3], 'c' => { 'd' => 4, 'e' => 5 } } { "a" => 1, "b" => [ [0] 2, [1] 3 ], "c" => { "d" => 4, "e" => 5 } } 

It looks like this when converting to YAML:

 irb(main):003:0> puts foo.to_yaml --- a: 1 b: - 2 - 3 c: d: 4 e: 5 

You can save this in a file:

 File.open('foo.yaml', 'w') { |fo| fo.puts foo.to_yaml } 

and read it back:

 bar = YAML.load_file('foo.yaml') { "a" => 1, "b" => [ [0] 2, [1] 3 ], "c" => { "d" => 4, "e" => 5 } } 

Similarly, using JSON:

 puts foo.to_json {"a":1,"b":[2,3],"c":{"d":4,"e":5}} 

You can save JSON in a file similar to YAML, and then reload it and re-include it in the hash. This is a slightly different syntax:

 irb(main):011:0> File.open('foo.json', 'w') { |fo| fo.puts foo.to_json } nil irb(main):012:0> bar = JSON.parse(File.read('foo.json')) { "a" => 1, "b" => [ [0] 2, [1] 3 ], "c" => { "d" => 4, "e" => 5 } } 

A big win in any language that can read YAML or JSON can read these files and use the data, either by reading OR, or writing them.

Performing a string replacement is very simple after you have the hash, because Ruby String gsub understands the hashes. This is from the docs :

If the second argument is a hash, and the matched text is one of its keys, the corresponding value is a replacement string.

This means you can do something like this:

 foo = 'Jackdaws love my giant sphinx of quartz' bar = { 'quartz' => 'rosy quartz', 'sphinx' => 'panda' } foo.gsub(Regexp.union(bar.keys), bar) "Jackdaws love my giant panda of rosy quartz" 

You need to keep track of the conflicts between the words that perform the wildcard, but the \b flag in the pattern may help. Doing this work is an exercise for the reader.

+21
source

See Marshal.load and Marshal.dump .

Alternatively, you can study the serialization of the hash in the yaml file and then read / save it back through YAML.

+8
source

I found this http://www.rubydoc.info/gems/bosh-director/1.1868.0/Bosh%2FDirector%2FConfig.load_hash after seeing how to download hashes myself.

In my example, we are dealing with MongoDB in a Rails application.

So, I set the Initialize method to get the collection that I will work with.

def initialize @collection = self.class.collection end

self.class is the class I'm participating in. let me use people as an example.

boot method next

 def load_collection(file_path) hash_values = self.class.load_hash(file_path) @collection.insert_many(hash) end 

inside bind.pry

 [1] pry(#<People>)> file_path => "people_results.json" [2] pry(#<People>)> hash => [{"number"=>0, "first_name"=>"SHAUN", "last_name"=>"JOHNSON", "gender"=>"M", "group"=>"15 to 19", "secs"=>1464}, {"number"=>1, "first_name"=>"TUAN", "last_name"=>"JOHNSON", "gender"=>"M", "group"=>"masters", "secs"=>3994}, {"number"=>2, "first_name"=>"EARLINE", "last_name"=>"BROWN", "gender"=>"F", "group"=>"masters", "secs"=>1415}... [3] pry(#<People>)> self => #<People:0x007fc80301b5f8 @coll=#<Mongo::Collection:0x70248510355560 namespace=test.people>> [4] pry(#<People>)> self.class => People 

So, in this case, the load_hash method takes a parameter, and in this case I transferred a file with some data to json. I then paste this data into my collection.

0
source

Source: https://habr.com/ru/post/914073/


All Articles