Author liubin http://www.ruby-cn.org/
This article address http://www.ruby-cn.org/articles/what_is_yaml.html
1.YAML = "YAML AIN't Markup Language"
The interpretation of the official website is YAML = "YAML AIN't Markup Language" (abbreviated as YAML). This is a data serialization language. It is a data structure for readable text. Its design goal is to make people readily read and proceed. It is similar to XML, but is simpler than XML.
2. Design goals:
The design objectives of YAML are as follows:
YAML document is easy to read. YAML stores data using local structures. YAML data is portable between programming languages. YAML has a fixed model to support a general development tool. YAML supports stream-based processing. YAML performance is strong and easy to expand. YAML is easy to implement and use.
3. And XML relationship
Novice always tries to understand the relationship between YAML and XML, both of which are in competition in certain areas, there is no direct relationship.
YAML is first a data serialization language, while XML is backward inheriting SGML (Standard Generalized Markup Language). XML tried to do it, it tried to be a document format, data format, message package format, secure RPC channel (SOAP) or even XML database; and YAML only focused on limited fields, it simply expressed in dynamic programming language ( The data structure and data types encountered in Perl, Python, Ruby. Currently, some binding / libraries have been made for these languages. It should be pointed out that we have begun to do XML / YAML mapping, more information can be accessed: http://yaml.org/xml/.
4.Yaml preliminary
The data in YAML is mainly represented by sequence, MAP (some also called Hash) and Scarar. The syntax is relatively simple, easy to read.
Note The starting sequence by "-" starts MAP with key: value "---" indicates that a YAML document starts List and Hash can nested the concept of block, a block is a text. Inline Collections: Data is written in one line
Example: 1. sequence
- Apple
- banana
- Carrot
2. Nested sequence
- Apple
-
- foo
- bar
- x123
- banana
- Carrot
3. Map
Foo: WhatVER
Bar: stuff
4. Map and sequence nested
Foo: WhatVER
Bar:
- UNO
- DOS
5. Map nested
Foo: WhatVER
Bar:
Fruit: Apple
Name: Steve
Sport: Baseball
6. Inline hash
---
Hash: {Name: Steve, Foo: bar}
5. YAML support in Ruby
Ruby1.8 already contains YAML support. Just need Require to come in.
one example:
# file name yaml.rb
Require 'Yaml'
Class Person
Attr_Accessor: Name,: SEX,: AGE,: Emaildef Initialize (N, S, A, E)
@ Name = n
@ sex = s
@ agn = a
@ email = e
end
end
# Create an object
Person = Person.new ("Liubin", "Male", 25, "Liubin@huangpuzhuang.com")
PUTS (Person.to_YAML) # Results after printing serialization
# Create an object from Person.yml
Person2 = yaml :: loading (file.open ('Person.yml'))
PUTS (Person2.to_YAML)
Puts person2.class # Results is Person
Puts person2.name
Person.yml file:
---! Ruby / Object: Yaml :: Person
AGE: 18
Email: xyz@huangpuzhuang.com
Name: XYZ
SEX: Female
Note that in Person.yml "---! Ruby / Object: Yaml :: Person" is indispensable, otherwise person2.class returns Hash, and Person2.name will also be wrong.
The execution of the program is as follows:
C: /> Ruby YAML.RB
---! Ruby / Object: Person
Age: 25
Email: liubin@huangpuzhuang.com
Name: liubin
SEX: MALE
---! Ruby / Object: Person
AGE: 18
Email: xyz@huangpuzhuang.com
Name: XYZ
SEX: Female
Person
xyz
Summary: YAML is a lot, not for a while, you can learn and clear, I hope we will learn together. Next, please see another article: http://www.ruby-cn.org/articles/yaml_in_5_minutes.html, maybe more about YAML.
Reference:
1.Ruby Language Homepage: http://www.ruby-lang.org/
2.yaml Home: http://yaml.org/