July 2011
Intermediate to advanced
400 pages
8h 46m
English
Another handy way of turning your Ruby objects into YAML format is provided by the dump method. At its simplest, this converts your Ruby data into YAML format and “dumps” it into a string:
yaml_dump1.rb
arr = ["fred", "bert", "mary"]
yaml_arr = YAML.dump( arr )
# yaml_arr is now: "--- \n- fred\n- bert\n- mary\n"More usefully, the dump method can take a second argument, which is some kind of IO object, typically a file. You can open a file and dump data to it:
yaml_dump2.rb
f = File.open( 'friends.yml', 'w' ) YAML.dump( ["fred", "bert", "mary"], f ) f.close
Or you can open the file (or some other type of IO object) and pass this into an associated block:
File.open( 'morefriends.yml', 'w' ){ |friendsfile| YAML.dump( ["sally", "agnes", ...Read now
Unlock full access