This page contains all of the examples from throughout this documentation. Convenient? Perhaps?
- dogs - cats - badgers
[ 'dogs', 'cats', 'badgers' ]
require 'yaml' puts( [ 'dogs', 'cats', 'badgers' ].to_yaml ) # prints: # - dogs # - cats # - badgers
- - pineapple - coconut - - umbrella - raincoat
dog: canine cat: feline badger: malign
{ 'dog' => 'canine',
'cat' => 'feline',
'badger' => 'malign' }
require 'yaml'
puts( { 'dog' => 'canine',
'cat' => 'feline',
'badger' => 'malign' }.to_yaml )
# prints:
# dog: canine
# cat: feline
# badger: malign
Joey: age: 22 sex: M Laura: age: 24 sex: F
- ~ - true - 10 - 10.2 - 2002-08-15T17:18:23.18-06:00 - 2002-08-15
~: NilClass +: TrueClass true: TrueClass True: TrueClass -: FalseClass false: FalseClass False: FalseClass 0: Integer 1: Integer 100: Integer 1,000: Integer 0.0: Float 1.0: Float 100.001: Float 1,000.001: Float 1.00009e+3: Float 2002-08-15T17:18:23.18-06:00: Time 2002-08-15 17:18:23.18 -06:00: Time 1976-07-31: Date
puts( nil.to_yaml ) # prints: # --- ~ puts( true.to_yaml ) # prints: # --- true puts( false.to_yaml ) # prints: # --- (false) puts( 10.to_yaml ) # prints: # --- 10 puts( 20.45.to_yaml ) # prints: # --- 20.45 puts( Time.now.to_yaml ) # prints: # --- 2002-08-15T17:29:01.79-06:00 puts( Date.new( 1976, 07, 31 ).to_yaml ) # prints: # --- 1976-07-31
- literal: |
A literal block keeps all
new lines when it is brought
into Ruby.
- folded: >
A folded block will get rid
of its newlines, trading them
for spaces when it is brought
into Ruby.
txt = "Just got my (dead-tree, printed-on-paper, I don't know if there's a web " +
"version) copy of Linux Magazine for September, 2002. There's an article by " +
"Dave Thomas about building networked applications in Ruby.\n\nProps to Dave!"
puts txt.to_yaml
# prints:
# --- >-
# Just got my (dead-tree, printed-on-paper, I don't know if there's a web version)
# copy of Linux Magazine for September, 2002. There's an article by Dave Thomas
# about building networked applications in Ruby.
#
#
# Props to Dave!
txt =<<EOF ZenWeb 2.11.0 has been released! I don't think I've remembered to announce any releases in a while, so this one is a tad different. Among the newest changes are: + relative url renderer + massively improved demo/docs :) EOF puts txt.to_yaml # prints: # --- | # # ZenWeb 2.11.0 has been released! # # I don't think I've remembered to announce any releases in a while, so # this one is a tad different. Among the newest changes are: # # + relative url renderer # + massively improved demo/docs # # :) #
simple symbol: !ruby/symbol Simple shortcut syntax: !ruby/sym Simple symbols in maps: !ruby/sym MapKey: !ruby/sym MapValue
puts :Simple.to_yaml # prints: # --- !ruby/sym Simple
normal range: !ruby/range 10..20 exclusive range: !ruby/range 11...20 negative range: !ruby/range -1..-5 ? !ruby/range 0..40 : range as a map key
floats: !ruby/range 10.2..21.5 dates: !ruby/range 2001-03-02..2001-09-11 timestamps: !ruby/range 2001-09-03T05:16:23Z..2001-09-04T07:08:13Z
starts with a b: !ruby/regexp '/^b/' ends with a z: !ruby/regexp '/z$/' search for a C or c: !ruby/regexp '/c/i'
puts( /^b/.to_yaml ) # prints: # --- !ruby/regexp "/^b/"
class Video
attr_accessor :title, :year, :rating
def initialize( t, y, r )
@title = t
@year = y
@rating = r
end
end
collection = [ Video.new( "Ishtar", 1987, 8.8 ), Video.new( "Dr. Strangelove", 1964, 10.0 ) ]
puts collection.to_yaml # prints: # - !ruby/object:Video # title: Ishtar # year: 1987 # rating: 8.8 # - !ruby/object:Video # title: Dr. Strangelove # year: 1964 # rating: 10.0
puts [[ 'Crispin', 'Glover' ]].to_yaml( :Indent => 4, :UseHeader => true, :UseVersion => true ) # prints: # --- %YAML:1.0 # - # - Crispin # - Glover
Indent: The default indentation to use when emitting (defaults to 2) Separator: The default separator to use between documents (defaults to '---') SortKeys: Sort Hash keys when emitting? (defaults to false) UseHeader: Display the YAML header when emitting? (defaults to false) UseVersion: Display the YAML version when emitting? (defaults to false) AnchorFormat: A formatting string for anchor IDs when emitting (defaults to 'id%03d') ExplicitTypes: Use explicit types when emitting? (defaults to false) BestWidth: The character width to use when folding text (defaults to 80) UseFold: Force folding of text when emitting? (defaults to false) UseBlock: Force all text to be literal when emitting? (defaults to false) Encoding: Unicode format to encode with (defaults to :Utf8; requires Iconv)
any Object#to_yaml method YAML::Stream.new YAML::Store.new YAML::emitter_proc
require 'yaml'
y = YAML::Store.new( "/tmp/yaml.store.1", :Indent => 2 )
y.transaction do
y['names'] = ['Crispin', 'Glover']
y['hello'] = {'hi' => 'hello', 'yes' => 'YES!!' }
end
hello: hi: hello yes: YES!! names: - Crispin - Glover
o = [ 'array', 'of', 'items' ] o2 = YAML::load( o.to_yaml ) # o2 and o should be equal
node = YAML::parse( <<EOY ) one: 1 two: 2 EOY puts node.type_id # prints: 'map' p node.value['one'] # prints key and value nodes: # [ #<YAML::YamlNode:0x8220278 @type_id="str", @value="one", @kind="scalar">, # #<YAML::YamlNode:0x821fcd8 @type_id="int", @value="1", @kind="scalar"> ]' # Mappings can also be accessed for just the value by accessing as a Hash directly p node['one'] # prints: #<YAML::YamlNode:0x821fcd8 @type_id="int", @value="1", @kind="scalar">
--- !rubyjunkies.com,2002-10-24/addressBook name: Bunbury Olsen phone: 801-090-0900 address: | 12 E. 400 S. SLC, UT 84020
YAML.add_domain_type( "rubyjunkies.com,2002-10-24", "addressBook" ) { |type, val|
# Do something with 'val' here
}
class AddressBook
attr_accessor :name, :phone, :address
end
YAML.add_domain_type( "rubyjunkies.com,2002-10-24", "addressBook" ) { |type, val|
YAML.object_maker( AddressBook, val )
}
class AddressBook
def to_yaml( opts = {} )
YAML.quick_emit( self.id, opts ) { |out|
out.map( "!rubyjunkies.com,2002-10-24" ) { |map|
instance_variables.sort.each { |iv|
map.add( iv[1..-1], instance_eval( iv ) )
}
}
}
end
end
def to_yaml_type "!rubyjunkies.com,2002-10-24/addressBook" end
def to_yaml_properties [ '@name', '@phone', '@address' ] end
readme = YAML::load( File.open( 'README' ) )
readme_doc = YAML::load_stream( File.open( 'README' ) ) puts readme_doc.documents[0]['title'] # prints: # YAML.rb
--- at: 2001-08-12 09:25:00.00 Z type: GET HTTP: '1.0' url: '/index.html' --- at: 2001-08-12 09:25:10.00 Z type: GET HTTP: '1.0' url: '/toc.html'
require 'yaml'
log = File.open( "/var/log/apache.yaml" )
yp = YAML::load_documents( log ) { |doc|
puts "#{doc['at']} #{doc['type']} #{doc['url']}"
}
YAML::Parser.new.parse( io )
d = YAML::Stream.new( :Indent => 4, :UseHeader => true ) d.add( 'one' ) d.add( 'two' ) d.add( 'three' ) puts d.emit # prints: # --- one # --- two # --- three
tree = YAML::parse( File.open( "README" ) ) puts tree.type_id # prints: # map title = tree.select( "/title" )[0] puts title.value # prints: # YAML.rb obj_tree = tree.transform puts obj_tree['title'] # prints: # YAML.rb
require 'yaml'
log = File.open( "/var/log/apache.yaml" )
yp = YAML::parse_documents( log ) { |tree|
at = tree.select('/at')[0].value
type = tree.select('/type')[0].value
puts "#{at} #{type}"
}
players = YAML::parse( <<EOY )
player:
- given: Sammy
family: Sosa
- given: Ken
family: Griffey
- given: Mark
family: McGwire
EOY
given = players.select( "/player/*/given" )
p given.transform
# prints:
# ["Sammy", "Ken", "Mark"]
YAML.add_domain_type( "hospital.com,2003", "Med" ) do |type, val| Medication.new( val ) end