# Saving with yaml

**URL:** <https://rubytalk.org/t/saving-with-yaml/68295>\
**Category:** ruby-talk\
**Created:** [6 August 2013 15:41 UTC](https://rubytalk.org/t/saving-with-yaml/68295 "2013-08-06T15:41:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [6 August 2013 15:41 UTC](https://rubytalk.org/t/saving-with-yaml/68295/1 "2013-08-06T15:41:00Z")

</div>

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",  
&nbsp;&nbsp;hunger: 10,  
&nbsp;&nbsp;life: 76  
&nbsp;&nbsp;}  
@settings = {display: "1360\*960",  
&nbsp;&nbsp;render: "high",  
&nbsp;&nbsp;saves: "manual"  
&nbsp;&nbsp;}

output = File.new("Saves.yaml", "w") do |file|  
&nbsp;&nbsp;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/](http://www.ruby-forum.com/).  
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> ruby-talk mailing list  
> [ruby-talk@ruby-lang.org](mailto:ruby-talk@ruby-lang.org)  
> [http://lists.ruby-lang.org/cgi-bin/mailman/listinfo/ruby-talk](http://lists.ruby-lang.org/cgi-bin/mailman/listinfo/ruby-talk)

---

<div class="post-metadata">

**Author:** ![Josh\_Cheek](https://avatars.discourse-cdn.com/v4/letter/j/e79b87/32.png) [@Josh\_Cheek](https://rubytalk.org/u/Josh_Cheek)\
**Post date:** [6 August 2013 18:17 UTC](https://rubytalk.org/t/saving-with-yaml/68295/2 "2013-08-06T18:17:45Z")

</div>

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"}}

> **···**
>
> On Tue, Aug 6, 2013 at 10:41 AM, Green Eco \<lists@ruby-forum.com\> 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",  
> > &nbsp;&nbsp;hunger: 10,  
> > &nbsp;&nbsp;life: 76  
> > &nbsp;&nbsp;}  
> > @settings = {display: "1360\*960",  
> > &nbsp;&nbsp;render: "high",  
> > &nbsp;&nbsp;saves: "manual"  
> > &nbsp;&nbsp;}
> > 
> > output = File.new("Saves.yaml", "w") do |file|  
> > &nbsp;&nbsp;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/\](http://www.ruby-forum.com/%5C).  
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > ruby-talk mailing list  
> > ruby-talk@ruby-lang.org  
> > [http://lists.ruby-lang.org/cgi-bin/mailman/listinfo/ruby-talk](http://lists.ruby-lang.org/cgi-bin/mailman/listinfo/ruby-talk)

---

<div class="post-metadata">

**Author:** ![Sandor\_Szuecs](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@Sandor\_Szuecs](https://rubytalk.org/u/Sandor_Szuecs)\
**Post date:** [6 August 2013 17:40 UTC](https://rubytalk.org/t/saving-with-yaml/68295/3 "2013-08-06T17:40:13Z")

</div>

For example:  
File.open("Saves.yaml", "w") do |file|  
&nbsp;&nbsp;file.write(YAML.dump(@characters))  
&nbsp;&nbsp;file.write(YAML.dump(@saves))  
&nbsp;&nbsp;file.write(YAML.dump(@settings))  
end

or

File.open("Saves.yaml", "w") do |file|  
&nbsp;&nbsp;[@characters,@saves,@settings].each do |item|  
&nbsp;&nbsp;&nbsp;&nbsp;file.write(YAML.dump(item))  
&nbsp;&nbsp;end  
end

or even better:

File.open("Saves.yaml", "w") do |file|  
&nbsp;&nbsp;file.write(YAML.dump([@characters,@saves,@settings]))  
end  
YAML.load File.read "Saves.yaml"  
# =\> [["Molly", "Trade"], {:location=\>"Southeaster", :hunger=\>10,  
:life=\>76}, {:display=\>"1360\*960", :render=\>"high", :saves=\>"manual"}]

> **···**
>
> On 8/6/13 5:41 PM, 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",  
> > &nbsp;&nbsp;hunger: 10,  
> > &nbsp;&nbsp;life: 76  
> > &nbsp;&nbsp;}  
> > @settings = {display: "1360\*960",  
> > &nbsp;&nbsp;render: "high",  
> > &nbsp;&nbsp;saves: "manual"  
> > &nbsp;&nbsp;}
> > 
> > output = File.new("Saves.yaml", "w") do |file|  
> > &nbsp;&nbsp;YAML.dump  
> > end  
> > output.write YAML.dump(@characters) # Wie sichert man mehrere
> 
> --  
> All the best, Sandor Szücs  
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> ruby-talk mailing list  
> ruby-talk@ruby-lang.org  
> [http://lists.ruby-lang.org/cgi-bin/mailman/listinfo/ruby-talk](http://lists.ruby-lang.org/cgi-bin/mailman/listinfo/ruby-talk)

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [7 August 2013 11:29 UTC](https://rubytalk.org/t/saving-with-yaml/68295/4 "2013-08-07T11:29:28Z")

</div>

Thank you guys, you really helped me out. Thanks 🙂

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).  
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> ruby-talk mailing list  
> [ruby-talk@ruby-lang.org](mailto:ruby-talk@ruby-lang.org)  
> [http://lists.ruby-lang.org/cgi-bin/mailman/listinfo/ruby-talk](http://lists.ruby-lang.org/cgi-bin/mailman/listinfo/ruby-talk)
