# Saving class with array property with YAML

**URL:** <https://www.ruby-forum.com/t/saving-class-with-array-property-with-yaml/84359>\
**Category:** Ruby\
**Created:** [February 3, 2007, 5:15pm UTC](https://www.ruby-forum.com/t/saving-class-with-array-property-with-yaml/84359 "2007-02-03T17:15:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Martin\_MSSS\_ller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/74df32/32.png) [@Martin\_MSSS\_ller](https://www.ruby-forum.com/u/Martin_MSSS_ller)\
**Post date:** [February 3, 2007, 5:15pm UTC](https://www.ruby-forum.com/t/saving-class-with-array-property-with-yaml/84359/1 "2007-02-03T17:15:47Z")

</div>

Hello

I’m new to Ruby and unfortuantly I can’t find an example anywhere to the  
following problem.

I have the following class:

class Article  
attr\_accessor :author  
attr\_accessor :title

def inititialize(author, title)  
@autor = author  
@title = title  
end  
end

class Book  
attr\_accessor :year  
attr\_accessor :volume  
attr\_accessor :articles # array of articles

def initialize(year, volume)  
@year = year  
@volume = volume  
@articles = Array.new  
end

def insert(author, title)  
@articles.push(Article.new(author,title))  
end  
end

class Bookshelf  
…  
end

#create book  
testbook = Book.new(2007, 1)

#insert 2 articles into the book  
testbook.insert(“Martin Mueller”, “Confessing that I’m a Ruby Dummy”)  
testbook.insert(“Hillary Clinton”, “Elect me and Ruby will save our  
planet”)

#write Book to YAML  
File.open(“testbook.yaml”, “w”) {|f| YAML.dump(testbook, f)}

* * *

My question is: how can I bring Ruby to save also the Array??

I played around with to\_yaml\_properties but without success.

thanks a lot for any hint!

Best regards,  
Martin.

---

<div class="post-metadata">

**Author:** ![Martin\_MSSS\_ller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/74df32/32.png) [@Martin\_MSSS\_ller](https://www.ruby-forum.com/u/Martin_MSSS_ller)\
**Post date:** [February 3, 2007, 5:46pm UTC](https://www.ruby-forum.com/t/saving-class-with-array-property-with-yaml/84359/2 "2007-02-03T17:46:51Z")

</div>

Hi Martin, you have a few issues in your code, I will go through them  
for you.

On 2/3/07, Martin Müller [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> attr\_accessor :title
> 
> def inititialize(author, title)

this should be spelled initialize, might be why you are getting an  
argument error

> testbook.insert(“Martin Mueller”, “Confessing that I’m a Ruby Dummy”)  
> testbook.insert(“Hillary Clinton”, “Elect me and Ruby will save our planet”)
> 
> #write Book to YAML

you also need to:  
require ‘yaml’

> File.open(“testbook.yaml”, “w”) {|f| YAML.dump(testbook, f)}

> 

Now it saves teh articles array

---

<div class="post-metadata">

**Author:** ![Martin\_MSSS\_ller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/74df32/32.png) [@Martin\_MSSS\_ller](https://www.ruby-forum.com/u/Martin_MSSS_ller)\
**Post date:** [February 5, 2007, 11:06am UTC](https://www.ruby-forum.com/t/saving-class-with-array-property-with-yaml/84359/3 "2007-02-05T11:06:13Z")

</div>

Thank you both for your kind help!

BR/Martin.

---

<div class="post-metadata">

**Author:** ![Martin\_MSSS\_ller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/74df32/32.png) [@Martin\_MSSS\_ller](https://www.ruby-forum.com/u/Martin_MSSS_ller)\
**Post date:** [February 3, 2007, 6:00pm UTC](https://www.ruby-forum.com/t/saving-class-with-array-property-with-yaml/84359/4 "2007-02-03T18:00:52Z")

</div>

On 03.02.2007 17:13, Martin Müller wrote:

> attr\_accessor :title  
> attr\_accessor :volume  
> end
> 
> My question is: how can I bring Ruby to save also the Array??
> 
> I played around with to\_yaml\_properties but without success.
> 
> thanks a lot for any hint!

First you should correct spelling errors of your initialize methods.  
After you did that, you don’t have to do anything:

irb(main):049:0\> testbook = Book.new(2007, 1)  
=\> #\<Book:0x4918fe4 @year=2007, @articles=[], @volume=1\>  
irb(main):050:0\> testbook.insert(“Martin Mueller”, “Confessing that I’m  
a Ruby Dummy”)  
=\> [#\<Article:0x4914e94 @title=“Confessing that I’m a Ruby Dummy”,  
@autor=“Martin Mueller”\>]  
irb(main):051:0\> testbook.insert(“Hillary Clinton”, “Elect me and Ruby  
will save our planet”)  
=\> [#\<Article:0x4914e94 @title=“Confessing that I’m a Ruby Dummy”,  
@autor=“Martin Mueller”\>, #\<Article:0x491090c @title=  
“Elect me and Ruby will save our planet”, @autor=“Hillary Clinton”\>]  
irb(main):052:0\> puts testbook.to\_yaml  
— !ruby/object:Book  
articles:

- !ruby/object:Article  
autor: Martin Mueller  
title: Confessing that I’m a Ruby Dummy
- !ruby/object:Article  
autor: Hillary Clinton  
title: Elect me and Ruby will save our planet  
volume: 1  
year: 2007  
=\> nil  
irb(main):053:0\>

Btw, your Book#insert does two things: create an Article and append that  
to your Book. It’s a convenient method. You don’t really need that  
method since you make #articles available anyway so you could as well  
do:

testbook.articles \<\< Article.new( “Martin”, “I am a Ruby” )

Kind regards

robert
