# Importing from YAML

**URL:** <https://www.ruby-forum.com/t/importing-from-yaml/69829>\
**Category:** Ruby\
**Created:** [September 24, 2006, 6:30pm UTC](https://www.ruby-forum.com/t/importing-from-yaml/69829 "2006-09-24T18:30:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![tobyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ebca7d/32.png) [@tobyr](https://www.ruby-forum.com/u/tobyr)\
**Post date:** [September 24, 2006, 6:30pm UTC](https://www.ruby-forum.com/t/importing-from-yaml/69829/1 "2006-09-24T18:30:20Z")

</div>

I’ve learned that a very quick and easy way to save a Ruby object to a  
file is to use YAML e.g.

```
def saveMyObject
	File.open(self.fileName,"a+") { |f|
		f.write(self.to_yaml)
		f.close
	}
end

```

However, the article which showed this did not then explain how this  
process could be reversed. I assume there must be a an equivalent  
simple use of file ‘read’, but a straight forward ‘read’ seems to give  
just a string, and I can find no ‘from\_yaml’ method whihc would read  
YAML-formatted text straight into a Ruby object (and thus re-create that  
object originally used to create the YAML file) . I’m sure there must  
be a quick and easy way to do this - any suggestions?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![tobyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ebca7d/32.png) [@tobyr](https://www.ruby-forum.com/u/tobyr)\
**Post date:** [September 24, 2006, 6:52pm UTC](https://www.ruby-forum.com/t/importing-from-yaml/69829/2 "2006-09-24T18:52:09Z")

</div>

Toby R. wrote:

> I’ve learned that a very quick and easy way to save a Ruby object to a  
> file is to use YAML e.g.

YAML#load (or its synonym #parse) loads object(s) from a YAML string.  
Have a look at the docs: [http://ruby-doc.org/core/classes/YAML.html](http://ruby-doc.org/core/classes/YAML.html)

Regards,  
Jordan

---

<div class="post-metadata">

**Author:** ![tobyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ebca7d/32.png) [@tobyr](https://www.ruby-forum.com/u/tobyr)\
**Post date:** [September 24, 2006, 7:04pm UTC](https://www.ruby-forum.com/t/importing-from-yaml/69829/3 "2006-09-24T19:04:05Z")

</div>

Jordan Callicoat wrote:  
…

> YAML#load (or its synonym #parse) loads object(s) from a YAML string.  
> Have a look at the docs: [http://ruby-doc.org/core/classes/YAML.html](http://ruby-doc.org/core/classes/YAML.html)

Many thanks Jordan. For anyone interested I’ve just given it a go and  
the reverse of the above code seems to be quite simply

```
def LoadMyObject
        myObject = YAML.load_file(self.fileName)
end

```

---

<div class="post-metadata">

**Author:** ![tobyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ebca7d/32.png) [@tobyr](https://www.ruby-forum.com/u/tobyr)\
**Post date:** [September 24, 2006, 7:12pm UTC](https://www.ruby-forum.com/t/importing-from-yaml/69829/4 "2006-09-24T19:12:32Z")

</div>

Oops, that should be ‘loadMyObject’ of course …

Toby R. wrote:

> Jordan Callicoat wrote:  
> …
> 
> > YAML#load (or its synonym #parse) loads object(s) from a YAML string.  
> > Have a look at the docs: [http://ruby-doc.org/core/classes/YAML.html](http://ruby-doc.org/core/classes/YAML.html)
> 
> Many thanks Jordan. For anyone interested I’ve just given it a go and  
> the reverse of the above code seems to be quite simply
> 
> def LoadMyObject  
> myObject = YAML.load\_file(self.fileName)  
> end

---

<div class="post-metadata">

**Author:** ![tobyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ebca7d/32.png) [@tobyr](https://www.ruby-forum.com/u/tobyr)\
**Post date:** [September 24, 2006, 7:52pm UTC](https://www.ruby-forum.com/t/importing-from-yaml/69829/5 "2006-09-24T19:52:14Z")

</div>

On Sep 24, 2006, at 12:04 PM, Toby R. wrote:

> ```
> myObject = YAML.load_file(self.fileName)
> 
> ```
> 
> end

No need to assign to a local variable there, since it goes out of  
scope as soon as you leave the method on the next line.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![tobyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ebca7d/32.png) [@tobyr](https://www.ruby-forum.com/u/tobyr)\
**Post date:** [September 24, 2006, 7:52pm UTC](https://www.ruby-forum.com/t/importing-from-yaml/69829/6 "2006-09-24T19:52:15Z")

</div>

On Sep 24, 2006, at 12:12 PM, Toby R. wrote:

> Oops, that should be ‘loadMyObject’ of course …

Actually, the Ruby convention is to methods and variables like\_this  
and classes and modules LikeThis.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![tobyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ebca7d/32.png) [@tobyr](https://www.ruby-forum.com/u/tobyr)\
**Post date:** [September 25, 2006, 1:30am UTC](https://www.ruby-forum.com/t/importing-from-yaml/69829/7 "2006-09-25T01:30:15Z")

</div>

Toby R. wrote:

> I’ve learned that a very quick and easy way to save a Ruby object to a  
> file is to use YAML e.g.
> 
> def saveMyObject  
> File.open(self.fileName,“a+”) { |f|  
> f.write(self.to\_yaml)  
> f.close  
> }  
> end

No-one mentioned it but the f.close in the File.open block  
is not required–the whole idea behind the block is to use  
it to close the file automatically after it has executed.
