From: Josh Cheek Date: 2013-08-06T13:17:45-05:00 Subject: Re: Saving with yaml --===============1625691570== Content-Type: multipart/alternative; boundary=089e0160a6f20b396604e34b71c9 --089e0160a6f20b396604e34b71c9 Content-Type: text/plain; charset=ISO-8859-1 On Tue, Aug 6, 2013 at 10:41 AM, Green Eco wrote: > Hi, > > I have a question. :-) I have already found a lot in the web but that > does not really helped me. This far my code looks like this: > > > to save: > > require "yaml" > > @characters = ["Molly", "Trade"] > @saves = {location: "Southeaster", > hunger: 10, > life: 76 > } > @settings = {display: "1360*960", > render: "high", > saves: "manual" > } > > output = File.new("Saves.yaml", "w") do |file| > YAML.dump > end > output.write YAML.dump(@characters) # Wie sichert man mehrere > Sachen?????? > output.close > > > and to load: > > require "yaml" > > output = File.new("Saves.yaml", "r") > p YAML.load(output.read) > output.close > > How can I save multiple array etc.? > > It works but I want to save more than one hash, I want to save e.g. all > of them, but with that I can save just one or if I manage it to save all > three I can just load the first one. > So it would be nice if someone could help me. :-) Thanks > > Yours faithfully > Greeneco > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > ruby-talk mailing list > ruby-talk@ruby-lang.org > http://lists.ruby-lang.org/cgi-bin/mailman/listinfo/ruby-talk > I'd probably just put them all in the same hash: require "yaml" filename = "data.yaml" characters = ["Molly", "Trade"] saves = {location: "Southeaster", hunger: 10, life: 76} settings = {display: "1360*960", render: "high", saves: "manual"} program_data = {characters: characters, saves: saves, settings: settings} # File.write may not be in some older versions of Ruby File.write filename, YAML.dump(program_data) loaded_data = YAML.load(File.read filename) require 'pp' pp loaded_data # >> {:characters=>["Molly", "Trade"], # >> :saves=>{:location=>"Southeaster", :hunger=>10, :life=>76}, # >> :settings=>{:display=>"1360*960", :render=>"high", :saves=>"manual"}} --089e0160a6f20b396604e34b71c9 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable On Tue, Aug 6, 2013 at 10:41 AM, Green Eco <lists@ruby-forum.com>= ; wrote:

--
Posted via http://= www.ruby-forum.com/.
_______________________________________________
ruby-talk mailing list
ruby-talk@ruby-lang.org
http://lists.ruby-lang.org/cgi-bin/mailman/listinfo/ruby-t= alk


I'd probably j= ust put them all in the same hash:

require &q= uot;yaml"

filename =A0 =A0 =3D "data.yam= l"
characters =A0 =3D ["Molly", "Trade"]
sa= ves =A0 =A0 =A0 =A0=3D {location: "Southeaster", hunger: 10, life= : 76}
settings =A0 =A0 =3D {display: "1360*960", render= : "high", saves: "manual"}
program_data =3D {characters: characters, saves: saves, settings: sett= ings}

# File.write may not be in some older versio= ns of Ruby
File.write filename, YAML.dump(program_data)

loaded_data =3D YAML.load(File.read filename)

=
require 'pp'
pp loaded_data
# >&g= t; {:characters=3D>["Molly", "Trade"],
# &= gt;> =A0:saves=3D>{:location=3D>"Southeaster", :hunger= =3D>10, :life=3D>76},
# >> =A0:settings=3D>{:display=3D>"1360*960", :r= ender=3D>"high", :saves=3D>"manual"}}
--089e0160a6f20b396604e34b71c9-- --===============1625691570== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ ruby-talk mailing list ruby-talk@ruby-lang.org http://lists.ruby-lang.org/cgi-bin/mailman/listinfo/ruby-talk --===============1625691570==--