[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Importing from YAML

Toby Rodwell

9/24/2006 4:30:00 PM

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.

--
Posted via http://www.ruby-....

6 Answers

MonkeeSage

9/24/2006 4:47:00 PM

0

Toby Rodwell 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...

Regards,
Jordan

Toby Rodwell

9/24/2006 5:04:00 PM

0

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...

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




--
Posted via http://www.ruby-....

Toby Rodwell

9/24/2006 5:13:00 PM

0

Oops, that should be 'loadMyObject' of course ...

Toby Rodwell 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...
>
> 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


--
Posted via http://www.ruby-....

James Gray

9/24/2006 5:50:00 PM

0

On Sep 24, 2006, at 12:04 PM, Toby Rodwell 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...
>
> 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

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 Gray II

James Gray

9/24/2006 5:52:00 PM

0

On Sep 24, 2006, at 12:12 PM, Toby Rodwell 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 Gray II

e

9/24/2006 11:30:00 PM

0

Toby Rodwell 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.


--
Posted via http://www.ruby-....