What are good examples of mapping YAML data to Ruby objects? - ruby

I am looking for basic examples of YAML syntax and how to work with it in Ruby.
Basically, by looking at the examples, I hope to better understand how to map YAML scalars to object attributes, and whether to use different YAML files or having one YAML file containing multiple objects.

There is a YAML class in Ruby core which has a short tutorial and a few links.
YAML in Five Minutes
Serializing and Deserializing objects with Ruby
require "yaml"
test_obj = ["dogs", "cats", "badgers"]
yaml_obj = YAML::dump( test_obj )
# -> ---
- dogs
- cats
- badgers
ruby_obj = YAML::load( yaml_obj )
# => ["dogs", "cats", "badgers"]
ruby_obj == test_obj
# => true

Related

Can I manipulate yaml files and write them out again

I have a map of values, the key is a filename and the value is an array strings.
I have the corresponding files
how would I load the file and create a fixed yaml value which contains the value of the array whether or not the value already exists
e.g.
YAML (file.yaml)
trg::azimuth:
-extra
-intra
-lateral
or
trg::azimuth:
[extra,intra,lateral]
from
RUBY
{"file.yaml" => ["extra","intra","lateral"]}
The YAML documentation doesn't cover its methods very well, but does say
The underlying implementation is the libyaml wrapper Psych.
The Psych documentation, which underlies YAML, covers reading, parsing, and emitting YAML.
Here's the basic process:
require 'yaml'
foo = {"file.yaml" => ["extra","intra","lateral"]}
bar = foo.to_yaml
# => "---\nfile.yaml:\n- extra\n- intra\n- lateral\n"
And here's what the generated, serialized bar variable looks like if written:
puts bar
# >> ---
# >> file.yaml:
# >> - extra
# >> - intra
# >> - lateral
That's the format a YAML parser needs:
baz = YAML.load(bar)
baz
# => {"file.yaml"=>["extra", "intra", "lateral"]}
At this point the hash has gone round-trip, from a Ruby hash, to a YAML-serialized string, back to a Ruby hash.
Writing YAML to a file is easy using Ruby's File.write method:
File.write(foo.keys.first, foo.values.first.to_yaml)
or
foo.each do |k, v|
File.write(k, v.to_yaml)
end
Which results in a file named "file.yaml", which contains:
---
- extra
- intra
- lateral
To read and parse a file, use YAML's load_file method.
foo = YAML.load_file('file.yaml')
# => ["extra", "intra", "lateral"]
"How do I parse a YAML file?" might be of use, as well as the other "Related" links on the right side of this page.

Annotating Ruby structures to include anchors/references on #to_yaml

I have some large hashes (>10⁵ keys) with interlocking structures. They're stored on disk as YAML. I'd like to avoid duplication by using anchors and references in the YAML, but I haven't been able to figure out if there's a way to do it implicitly in the hash such that the #to_yaml method will label the anchor nodes properly.
Desired YAML:
---
parent1:
common-element-1: &CE1
complex-structure-goes: here
parent2:
uncomment-element-1:
blah: blah
<<: *CE1
Ruby code:
hsh = {
'parent1' => {
'common-element-1' => {
'complex-structure-goes' => 'here',
},
'parent2' => {
'uncommon-element-1' => {
'blah' => 'blah',
},
'<<' => '*CE1',
},
}
The reference is quite straightforward -- but how to embed the &CE1 anchor in the 'common-element-1' item in the Ruby hash?
I want to work as much as possible with native Ruby primitive types (like Hash) rather than mucking about with builders and emitters and such -- and I definitely don't want to write the YAML manually!
I've looked at Read and write YAML files without destroying anchors and aliases? and its relative, among other places, but haven't found an answer yet -- at least not that I've understood.
Thanks!
If you use the same Ruby object, the YAML library will set up references for you:
> common = {"ohai" => "I am common"}
> doc = {"parent1" => {"id" => 1, "stuff" => common}, "parent2" => {"id" => 2, "stuff" => common}}
> puts doc.to_yaml
---
parent1:
id: 1
stuff: &70133422893680
ohai: I am common
parent2:
id: 2
stuff: *70133422893680
I'm not sure there's a straightforward way of defining Hashes that are subsets of each other, though. Perhaps tweaking your structure a bit would be warranted?

Mapping XML to Ruby objects

I want to communicate between ruby and other applications in XML. I have defined a schema for this communication and I'm looking for the best way to do the transformation from data in Ruby to the XML and vice versa.
I have an XML document my_document.xml:
<myDocument>
<number>1</number>
<distance units="km">20</distance>
</myDocument>
Which conforms to an Schema my_document_type.xsd (I shalln't bother writing it out here).
Now I'd like to have the following class automatically generated from the XSD - is this reasonable or feasible?
# Represents a document created in the form of my_document_type.xsd
class MyDocument
attr_accessor :number, :distance, :distance_units
# Allows me to create this object from data in Ruby
def initialize(data)
#number = data['number']
#distance = data['distance']
#distance_units = data['distance_units']
end
# Takes an XML document of the correct form my_document.xml and populates internal systems
def self.from_xml(xml)
# Reads the XML and populates:
doc = ALibrary.load(xml)
#number = doc.xpath('/number').text()
#distance = doc.xpath('/distance').text()
#distance_units = doc.xpath('/distance').attr('units') # Or whatever
end
def to_xml
# Jiggery pokery
end
end
So that now I can do:
require 'awesomelibrary'
awesome_class = AwesomeLibrary.load_from_xsd('my_document_type.xsd')
doc = awesome_class.from_xml('my_document.xml')
p doc.distance # => 20
p doc.distance_units # => 'km'
And I can also do
doc = awesome_class.new('number' => 10, 'distance_units' => 'inches', 'distance' => '5')
p doc.to_xml
And get:
<myDocument>
<number>10</number>
<distance units="inches">5</distance>
</myDocument>
This sounds like fairly intense functionality to me, so I'm not expecting a full answer, but any tips as to libraries which already do this (I've tried using RXSD, but I can't figure out how to get it to do this) or any feasibility thoughts and so on.
Thanks in advance!
Have you tried Nokogiri? The Slop decorator implements method_missing into the document in such a way that it essentially duplicates the functionality you're looking for.

Parsing a .cfg file using Ruby?

I have a .cfg file with the following data:
*.*.key_val = {
key1= "value1";
key2 = "value2";
key3 = "value3";
};
I want to read this file and store the key value pairs in a hash #var[key][val].
How it can be done?
THIS cfg you may parse in such way:
read file using File#read
convert text into 2-dimentional array using String#scan and regex
convert array into hash using Hash[]
text = File.read('your.cfg')
# => "*.*.key_val = {\n key1= \"value1\";\n key2 = \"value2\";\n key3 = \"value3\";\n};"
data = text.scan(/(\S+)\s*=\s*"([^"]+)/)
# => [["key1", "value1"], ["key2", "value2"], ["key3", "value3"]]
#var = Hash[data]
# => {"key1"=>"value1", "key2"=>"value2", "key3"=>"value3"}
Or just:
#var = Hash[File.read('your.cfg').scan(/(\S+)\s*=\s*"([^"]+)/)]
I'd strongly recommend transferring the configuration to something like YAML. It's made to be easy to understand, flexible, universally implemented, well documented, both as a standard and as a part of the core library, and easy to understand. (Yes, I said it twice on purpose.)
My YAML files load into Ruby as a Hash when I do something like:
require 'yaml'
config = YAML.load_file('/path/to/config/file')
I'll create the initial template for the configuration file in Ruby, as a Hash, then serialize it and write it to disk. That way I know what's on the disk is exactly the way YAML wants it to be and helps me avoid that "it won't load because either the data is wrong or the code is wrong" quandary.
# A simple round-trip (load and dump) of an object.
require 'yaml'
test_obj = {
'foo' => 'bar',
'one_two_three' => [1, 2, 3],
'hash' => {'another' => 'hash'}
} #=> {"foo"=>"bar", "one_two_three"=>[1, 2, 3], "hash"=>{"another"=>"hash"}}
File.open('./config.yaml', 'w') { |fo| fo.puts YAML::dump( test_obj ) } #=> nil
ruby_obj = YAML::load_file( './config.yaml' ) #=> {"foo"=>"bar", "one_two_three"=>[1, 2, 3], "hash"=>{"another"=>"hash"}}
ruby_obj == test_obj #=> true
require 'pp'
pp ruby_obj
{"foo"=>"bar", "one_two_three"=>[1, 2, 3], "hash"=>{"another"=>"hash"}}
pp test_obj
{"foo"=>"bar", "one_two_three"=>[1, 2, 3], "hash"=>{"another"=>"hash"}}
You should try out the 'parseconfig' gem: https://rubygems.org/gems/parseconfig/
gem install parseconfig
Here's a sample how to use this gem:
require 'rubygems'
require 'parseconfig'
my_config = ParseConfig.new('your_file.cfg')
puts my_config.get_value('key_val')
Good luck and have fun learning Ruby. :)
EDIT
As Glenux said this is only for simple configuration files. I'll check if I can find anything else.
EDIT 2
I can't find a gem or something to parse a cfg file like in your example. I guess your only option is to write a parser yourself (like Nakilon did) or use something like YAML instead. Good luck anyway. :)
The parseconfig class is only intended for simple configuration files !
It accepts files of the format "param = value" (cf http://www.5dollarwhitebox.org/drupal/projects#rb-parseconfig ) but it will not parse the *.*.key_val = { and } thing.
Is the configuration file yours or generated/used by a third-party software? If it is yours, it may be wiser to use an other configuration file format (JSON, Ini, YAML, etc).
Wanted to mention this ruby library that can help you transfer configuration between JSON, YAML or Windows Ini file formats:
https://github.com/kigster/dupervisor
The use case is to move the configuration to YAML, but be able to generate whatever the format is needed by the software – in the case of this gem – it's supervisord's INI file format.

how to store a Ruby array into a file?

How to store a Ruby array into a file?
I am not sure what exactly you want, but, to serialize an array, write it to a file and read back, you can use this:
fruits = %w{mango banana apple guava}
=> ["mango", "banana", "apple", "guava"]
serialized_array = Marshal.dump(fruits)
=> "\004\b[\t\"\nmango\"\vbanana\"\napple\"\nguava"
File.open('/tmp/fruits_file.txt', 'w') {|f| f.write(serialized_array) }
=> 33
# read the file back
fruits = Marshal.load File.read('/tmp/fruits_file.txt')
=> ["mango", "banana", "apple", "guava"]
There are other alternatives you can explore, like json and YAML.
To just dump the array to a file in the standard [a,b,c] format:
require 'pp'
$stdout = File.open('path/to/file.txt', 'w')
pp myArray
That might not be so helpful, perhaps you might want to read it back? In that case you could use json. Install using rubygems with gem install json.
require 'rubygems'
require 'json'
$stdout = File.open('path/to/file.txt', 'w')
puts myArray.to_json
Read it back:
require 'rubygems'
require 'json'
buffer = File.open('path/to/file.txt', 'r').read
myArray = JSON.parse(buffer)
There are multiple ways to dump an array to disk. You need to decide if you want to serialize in a binary format or in a text format.
For binary serialization you can look at Marshal
For text format you can use json, yaml, xml (with rexml, builder, ... ) , ...
Some standard options for serializing data in Ruby:
Marshal
YAML
JSON (built-in as of 1.9, various gems available as well)
(There are other, arguably better/faster implementations of YAML and JSON, but I'm linking to built-ins for a start.)
In practice, I seem to see YAML most often, but that may not be indicative of anything real.
Here's a quick yaml example
config = {"rank" => "Admiral", "name"=>"Akbar",
"wallet_value" => 9, "bills" => [5,1,1,2]}
open('store.yml', 'w') {|f| YAML.dump(config, f)}
loaded = open('store.yml') {|f| YAML.load(f) }
p loaded
# => {"name"=>"Akbar", "wallet_value"=>9, \
# "bills"=>[5, 1, 1, 2], "rank"=>"Admiral"}
Example: write text_area to a file where text_area is an array of strings.
File.open('output.txt', 'w') { |f| text_area.each { |line| f << line } }
Don't forget to do error checking on file operations :)
Afaik.. files contain lines not arrays. When you read the files, the data can then be stored in an array or other data structures. I am anxious to know if there is another way.

Resources