Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456 YAML.rb is YAML for Ruby | Cookbook
Welcome to the Yaml Cookbook for Ruby. This version of the Yaml Cookbook focuses on the
Ruby implementation of Yaml by comparing Yaml documents with their Ruby counterparts.
YAML(tm) is a readable text format for data structures. As you'll
see below, YAML can handle many common data types and structures. And what
YAML can't handle natively can be supported through flexible type families.
For example, YAML for Ruby uses type families to support storage of regular
expressions, ranges and object instances.
You can add a keyed list (also known as a dictionary or
hash) to your document by placing each member of the
list on a new line, with a colon seperating the key
from its value. In YAML, this type of list is called
a mapping.
A merge key ('<<') can be used in a mapping to insert other mappings. If
the value associated with the merge key is a mapping, each of its key/value
pairs is inserted into the current mapping.
Mapping can also be contained on a single line, using the inline syntax. Each key-value pair is separated by a colon, with a comma between each entry in the mapping. Enclose with curly braces.
Any group of characters beginning with an alphabetic or numeric character is a string, unless it belongs to one of the groups below (such as an Integer or Time).
A string can contain any alphabetic or numeric character, along with many punctuation characters, including the period, dash, space, quotes, exclamation, and question mark.
Yaml
String characters
in YAML?
- What's Yaml?
- It's for writing data structures in plain text.
- And?
- And what? That's not good enough for you?
- No, I mean, "And what about Yaml?"
- Oh, oh yeah. Uh.. Yaml for Ruby.
Ruby
String characters
in Ruby?
[
"What's Yaml?",
"It's for writing data structures in plain text.",
"And?",
"And what? That's not good enough for you?",
"No, I mean, \"And what about Yaml?\"",
"Oh, oh yeah. Uh.. Yaml for Ruby."
]
Be careful using indicators in strings. In particular, the comma, colon, and pound sign must be used carefully.
Yaml
Indicators in Strings
in YAML?
the colon followed by space is an indicator: but is a string:right here
same for the pound sign: here we have it#in a string
the comma can, honestly, be used in most cases: [ but not in, inline collections ]
Ruby
Indicators in Strings
in Ruby?
{
'the colon followed by space is an indicator' => 'but is a string:right here',
'same for the pound sign' => 'here we have it#in a string',
'the comma can, honestly, be used in most cases' => [ 'but not in', 'inline collections' ]
}
You can also enclose your strings within single quotes, which allows use of slashes, colons, and other indicators freely. Inside single quotes, you can represent a single quote in your string by using two single quotes next to each other.
Yaml
Single-quoted Strings
in YAML?
all my favorite symbols: '#:!/%.)'
a few i hate: '&(*'
why do i hate them?: 'it''s very hard to explain'
Ruby
Single-quoted Strings
in Ruby?
{
'all my favorite symbols' => '#:!/%.)',
'a few i hate' => '&(*',
'why do i hate them?' => 'it\'s very hard to explain'
}
Both single- and double-quoted strings may be carried on to new lines in your YAML document. They must be indented a step and indentation is interpreted as a single space.
Yaml
Multi-line Quoted Strings
in YAML?
i want a long string: "so i'm going to
let it go on and on to other lines
until i end it with a quote."
Ruby
Multi-line Quoted Strings
in Ruby?
{ 'i want a long string' => "so i'm going to " +
"let it go on and on to other lines " +
"until i end it with a quote."
}
Unquoted strings may also span multiple lines, if they are free of YAML space indicators and indented.
Yaml
Plain scalars
in YAML?
- My little toe is broken in two places;
- I'm crazy to have skied this way;
- I'm not the craziest he's seen, since there was always the German guy
who skied for 3 hours on a broken shin bone (just below the kneecap);
- Nevertheless, second place is respectable, and he doesn't
recommend going for the record;
- He's going to put my foot in plaster for a month;
- This would impair my skiing ability somewhat for the
duration, as can be imagined.
Ruby
Plain scalars
in Ruby?
[
"My little toe is broken in two places;",
"I'm crazy to have skied this way;",
"I'm not the craziest he's seen, since there was always " +
"the German guy who skied for 3 hours on a broken shin " +
"bone (just below the kneecap);",
"Nevertheless, second place is respectable, and he doesn't " +
"recommend going for the record;",
"He's going to put my foot in plaster for a month;",
"This would impair my skiing ability somewhat for the duration, " +
"as can be imagined."
]
A pipe character, followed by an indented block of text is treated as a literal block, in which newlines are preserved throughout the block, including the final newline.
To give you more control over how space is preserved in text blocks, YAML has the keep '+' and chomp '-' indicators. The keep indicator will preserve all ending newlines, while the chomp indicator will strip all ending newlines.
Yaml
Three trailing newlines in literals
in YAML?
clipped: |
This has one newline.
same as "clipped" above: "This has one newline.\n"
stripped: |-
This has no newline.
same as "stripped" above: "This has no newline."
kept: |+
This has four newlines.
same as "kept" above: "This has four newlines.\n\n\n\n"
Ruby
Three trailing newlines in literals
in Ruby?
{
'clipped' => "This has one newline.\n",
'same as "clipped" above' => "This has one newline.\n",
'stripped' => 'This has no newline.',
'same as "stripped" above' => 'This has no newline.',
'kept' => "This has four newlines.\n\n\n\n",
'same as "kept" above' => "This has four newlines.\n\n\n\n"
}
Normally, only a single newline is kept from the end of a literal block, unless the keep '+' character is used in combination with the pipe. The following example will preserve all ending whitespace since the last line of both literal blocks contains spaces which extend past the indentation level.
A greater-then character, followed by an indented block of text is treated as a folded block, in which lines of text separated by a single newline are concatenated as a single line.
Yaml
Folded Block in a Sequence
in YAML?
---
- apple
- banana
- >
can't you see
the beauty of yaml?
hmm
- dog
Ruby
Folded Block in a Sequence
in Ruby?
[
'apple',
'banana',
"can't you see the beauty of yaml? hmm\n",
'dog'
]
The keep and chomp indicators can also be applied to folded blocks.
Yaml
Three trailing newlines in folded blocks
in YAML?
clipped: >
This has one newline.
same as "clipped" above: "This has one newline.\n"
stripped: >-
This has no newline.
same as "stripped" above: "This has no newline."
kept: >+
This has four newlines.
same as "kept" above: "This has four newlines.\n\n\n\n"
Ruby
Three trailing newlines in folded blocks
in Ruby?
{
'clipped' => "This has one newline.\n",
'same as "clipped" above' => "This has one newline.\n",
'stripped' => 'This has no newline.',
'same as "stripped" above' => 'This has no newline.',
'kept' => "This has four newlines.\n\n\n\n",
'same as "kept" above' => "This has four newlines.\n\n\n\n"
}
If you need to refer to the same item of data twice, you can give that item an alias. The alias is a plain string, starting with an ampersand. The item may then be referred to by the alias throughout your document by using an asterisk before the name of the alias. This is called an anchor.
Yaml
Simple Alias Example
in YAML?
- &showell; Steve
- Clark
- Brian
- Oren
- *showell
The Ruby Struct class is registered as a YAML builtin type through Ruby, so it can safely be serialized. To use it, first make sure you define your Struct with Struct::new. Then, you are able to serialize with Struct#to_yaml and unserialize from a YAML stream.
Yaml
Struct class
in YAML?
--- !ruby/struct:BookStruct
author: Yukihiro Matsumoto
title: Ruby in a Nutshell
year: 2002
isbn: 0-596-00214-9
YAML has generic support for serializing objects from any class available in Ruby. If using the generic object serialization, no extra code is needed.
Yaml
Objects
in YAML?
--- !ruby/object:YAML::Zoolander
name: Derek
look: Blue Steel
Ruby
Objects
in Ruby?
class Zoolander
attr_accessor :name, :look
def initialize( look )
@name = "Derek"
@look = look
end
def ==( z )
self.name == z.name and self.look == z.look
end
end
When extending the Hash class, your instances of such a class will dump as YAML maps, tagged with a class name.
Yaml
Extending Kernel::Hash
in YAML?
--- !ruby/hash:YAML::MyHash
Black Francis: Frank Black
Kim Deal: Breeders
Joey Santiago: Martinis
Ruby
Extending Kernel::Hash
in Ruby?
# Note that the @me attribute isn't dumped
# because the default to_yaml is trained
# to dump as a regular Hash.
class MyHash < Kernel::Hash
attr_accessor :me
def initialize
@me = "Why"
end
end