# Use yaml to store Ruby arrays or hashes in the DB?

**URL:** <https://rubytalk.org/t/use-yaml-to-store-ruby-arrays-or-hashes-in-the-db/14430>\
**Category:** ruby-talk\
**Created:** [27 October 2004 12:09 UTC](https://rubytalk.org/t/use-yaml-to-store-ruby-arrays-or-hashes-in-the-db/14430 "2004-10-27T12:09:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![George\_Moschovitis1](https://avatars.discourse-cdn.com/v4/letter/g/e19adc/32.png) [@George\_Moschovitis1](https://rubytalk.org/u/George_Moschovitis1)\
**Post date:** [27 October 2004 12:09 UTC](https://rubytalk.org/t/use-yaml-to-store-ruby-arrays-or-hashes-in-the-db/14430/1 "2004-10-27T12:09:06Z")

</div>

Hello everybody,

the current version of NDB stores Array/Hash/GeneralRubyObjects using  
the Marshal.dump/load methods. This is relatively fast but not portable.  
Ie it is difficult to use the database from an application coded in  
another language due to Marshal being Ruby specific.

I could use yaml of course but I am wondering if this is fast enough.

Any opinions on this?

best regards,  
George Moschovitis

> **···**
>
> --  
> [www.navel.gr](http://www.navel.gr) | tel: +30 2106898050 | fax: +30 2106898437
> 
> web appliction engine: [http://www.navel.gr/nitro](http://www.navel.gr/nitro)  
> have fun: [http://www.joy.gr](http://www.joy.gr)

---

<div class="post-metadata">

**Author:** ![Zev\_Blut](https://avatars.discourse-cdn.com/v4/letter/z/ac8455/32.png) [@Zev\_Blut](https://rubytalk.org/u/Zev_Blut)\
**Post date:** [28 October 2004 09:43 UTC](https://rubytalk.org/t/use-yaml-to-store-ruby-arrays-or-hashes-in-the-db/14430/2 "2004-10-28T09:43:45Z")

</div>

Hello,

> the current version of NDB stores Array/Hash/GeneralRubyObjects using  
> the Marshal.dump/load methods. This is relatively fast but not portable.  
> Ie it is difficult to use the database from an application coded in  
> another language due to Marshal being Ruby specific.
> 
> I could use yaml of course but I am wondering if this is fast enough.
> 
> Any opinions on this?

That depends upon what you consider fast enough?  
If you read the Ruby-Talk thread starting at [99960] you can see some  
benchmarks.

[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/99960](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/99960)

REXML.

But, you need to watch out about what you store as YAML. If you are  
only just storing Arrays and Hashes of Strings, Numbers and other such  
things you are probably fine, but as soon as custom Ruby Objects start  
appearing you may be in trouble.

I have learned this the hard way, when working with objects that  
dynamically extend modules. Attached is a simple example of an  
interesting difference between Marshal and YAML, with regards to  
saving an objects state.

I hope this helps.  
Best,  
Zev

> **···**
>
> On Wed, 27 Oct 2004 21:09:06 +0900, George Moschovitis \<gm@navel.gr\> wrote:  
> From my experience YAML is much faster than an XML equivalent using  
> -----  
> Example code that shows that Marshal can store the modules extended  
> by a specific object, while YAML cannot.  
> -----  
> module TestModule  
> &nbsp;&nbsp;&nbsp;def foo  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "bar!"  
> &nbsp;&nbsp;&nbsp;end  
> end
> 
> x = "String"  
> x.extend TestModule
> 
> puts "Is x a TestModule?"  
> puts x.is\_a?(TestModule)
> 
> y = "String y"  
> puts "Is y a TestModule?"  
> puts y.is\_a?(TestModule)
> 
> include Marshal
> 
> puts "Dumping and Reloading X"  
> rx = restore(dump(x))  
> puts "Is the reloaded X a TestModule?"  
> puts rx.is\_a?(TestModule)  
> puts "Just to make sure we can still foo."  
> puts rx.foo  
> puts "Dumping and Reloading Y (for fun)."  
> ry = restore(dump(y))  
> puts "Is the reloaded Y a TestModule? (I hope not.)"  
> puts ry.is\_a?(TestModule)
> 
> puts "Now let's try the equivalent in YAML"  
> require "yaml"  
> yx = YAML.load(x.to\_yaml)  
> puts "Is the YAML reloaded X a TestModule?"  
> puts yx.is\_a?(TestModule)

---

<div class="post-metadata">

**Author:** ![James\_Britt6](https://avatars.discourse-cdn.com/v4/letter/j/a87d85/32.png) [@James\_Britt6](https://rubytalk.org/u/James_Britt6)\
**Post date:** [2 November 2004 03:20 UTC](https://rubytalk.org/t/use-yaml-to-store-ruby-arrays-or-hashes-in-the-db/14430/3 "2004-11-02T03:20:10Z")

</div>

George Moschovitis wrote:

> Hello everybody,
> 
> the current version of NDB stores Array/Hash/GeneralRubyObjects using  
> the Marshal.dump/load methods. This is relatively fast but not portable.  
> Ie it is difficult to use the database from an application coded in  
> another language due to Marshal being Ruby specific.
> 
> I could use yaml of course but I am wondering if this is fast enough.

How can you store Ruby-specific objects (i.e., custom, user-defined classes) in a language-neutral way?

James

---

<div class="post-metadata">

**Author:** ![T\_Onoma](https://avatars.discourse-cdn.com/v4/letter/t/c2a13f/32.png) [@T\_Onoma](https://rubytalk.org/u/T_Onoma)\
**Post date:** [2 November 2004 03:46 UTC](https://rubytalk.org/t/use-yaml-to-store-ruby-arrays-or-hashes-in-the-db/14430/4 "2004-11-02T03:46:38Z")

</div>

You use YAML.

No really. That's all there it to it. If it's not fast enough I'd advice  
writing your own YAML parser in Assembly, cause honestly it is a high level  
task and it is the only real portable solution. (Okay, there are some XML  
based tools but they will be just as slow.)

Of course you could use an OODB. Look up 'purple' in RAA. That in itself will  
interest you probably. But then read the included docs (README I think it is)  
there you will find caveats, in which it will suggest an OODB alternative.  
Maybe that will work.

Have Fun,  
T.

> **···**
>
> On Monday 01 November 2004 10:20 pm, James Britt wrote:
> 
> > George Moschovitis wrote:  
> > \> Hello everybody,  
> > \>  
> > \> the current version of NDB stores Array/Hash/GeneralRubyObjects using  
> > \> the Marshal.dump/load methods. This is relatively fast but not portable.  
> > \> Ie it is difficult to use the database from an application coded in  
> > \> another language due to Marshal being Ruby specific.  
> > \>  
> > \> I could use yaml of course but I am wondering if this is fast enough.
> > 
> > How can you store Ruby-specific objects (i.e., custom, user-defined  
> > classes) in a language-neutral way?

---

<div class="post-metadata">

**Author:** ![James\_Britt6](https://avatars.discourse-cdn.com/v4/letter/j/a87d85/32.png) [@James\_Britt6](https://rubytalk.org/u/James_Britt6)\
**Post date:** [2 November 2004 04:01 UTC](https://rubytalk.org/t/use-yaml-to-store-ruby-arrays-or-hashes-in-the-db/14430/5 "2004-11-02T04:01:57Z")

</div>

trans. (T. Onoma) wrote:

> \>  
> \> How can you store Ruby-specific objects (i.e., custom, user-defined  
> \> classes) in a language-neutral way?
> 
> You use YAML.
> 
> No really. That's all there it to it.

The ri data files for 1.8.2 are in YAML. Can I load the following into a YAML parser in, say, perl or python? How does that language make sense of it?

--- !ruby/object:RI::MethodDescription  
aliases:   
block\_params:  
comment:  
&nbsp;&nbsp;&nbsp;- !ruby/struct:SM::Flow:😛  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;body: "Exclusive Or---If \<em\>obj\</em\> is \<tt\>nil\</tt\> or \<tt\>false\</tt\>, returns \<tt\>false\</tt\>; otherwise, returns \<tt\>true\</tt\>."  
full\_name: FalseClass#^  
is\_singleton: false  
name: "^"  
params: \>  
&nbsp;&nbsp;&nbsp;false ^ obj =\> true or false

&nbsp;&nbsp;&nbsp;nil ^ obj =\> true or false

visibility: public

> **···**
>
> > On Monday 01 November 2004 10:20 pm, James Britt wrote:

---

<div class="post-metadata">

**Author:** ![George\_Moschovitis1](https://avatars.discourse-cdn.com/v4/letter/g/e19adc/32.png) [@George\_Moschovitis1](https://rubytalk.org/u/George_Moschovitis1)\
**Post date:** [2 November 2004 08:33 UTC](https://rubytalk.org/t/use-yaml-to-store-ruby-arrays-or-hashes-in-the-db/14430/6 "2004-11-02T08:33:50Z")

</div>

trans. (T. Onoma) wrote:

> **···**
>
> > On Monday 01 November 2004 10:20 pm, James Britt wrote:  
> > \> George Moschovitis wrote:  
> > \> \> Hello everybody,  
> > \> \>  
> > \> \> the current version of NDB stores Array/Hash/GeneralRubyObjects using  
> > \> \> the Marshal.dump/load methods. This is relatively fast but not portable.  
> > \> \> Ie it is difficult to use the database from an application coded in  
> > \> \> another language due to Marshal being Ruby specific.  
> > \> \>  
> > \> \> I could use yaml of course but I am wondering if this is fast enough.  
> > \>  
> > \> How can you store Ruby-specific objects (i.e., custom, user-defined  
> > \> classes) in a language-neutral way?
> > 
> > You use YAML.
> 
> yeap the latest version uses YAML instead of Marshal. Havent run any  
> benchmarks yet.
> 
> -g.
> 
> --  
> www.navel.gr | tel: +30 2106898050 | fax: +30 2106898437
> 
> web appliction engine: [http://www.navel.gr/nitro](http://www.navel.gr/nitro)  
> have fun: [http://www.joy.gr](http://www.joy.gr)

---

<div class="post-metadata">

**Author:** ![T\_Onoma](https://avatars.discourse-cdn.com/v4/letter/t/c2a13f/32.png) [@T\_Onoma](https://rubytalk.org/u/T_Onoma)\
**Post date:** [2 November 2004 04:17 UTC](https://rubytalk.org/t/use-yaml-to-store-ruby-arrays-or-hashes-in-the-db/14430/7 "2004-11-02T04:17:53Z")

</div>

You'll have to use types to get rid of the !ruby/object cruft.

&nbsp;&nbsp;YAML.add\_private\_type( 'MehthodDescription' ) { |type, val|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RI::MethodDescrition.new( val )  
&nbsp;&nbsp;}

That'll load it automatically. Note that val is the parsed document (a hash)  
so your initialize method will either have to deal with that ot you'll need a  
new constructor method instead. I usually define #initialize\_yaml(val) and  
use that.

To output the type correctly use:

&nbsp;&nbsp;class RI::MethodDescription

&nbsp;&nbsp;&nbsp;&nbsp;def to\_yaml\_type  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'MethodDescription'  
&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;end

Perl and Python should be able to handle no problem from there.

HTH,  
T.

P.S. You can also use a Domain type if you prefer. Form ore info see:

&nbsp;&nbsp;[http://yaml4r.sourceforge.net/doc/](http://yaml4r.sourceforge.net/doc/)

> **···**
>
> On Monday 01 November 2004 11:01 pm, James Britt wrote:
> 
> > trans. (T. Onoma) wrote:  
> > \> On Monday 01 November 2004 10:20 pm, James Britt wrote:  
> > \> \> How can you store Ruby-specific objects (i.e., custom, user-defined  
> > \> \> classes) in a language-neutral way?  
> > \>  
> > \> You use YAML.  
> > \>  
> > \> No really. That's all there it to it.
> > 
> > The ri data files for 1.8.2 are in YAML. Can I load the following into  
> > a YAML parser in, say, perl or python? How does that language make  
> > sense of it?
> > 
> > --- !ruby/object:RI::MethodDescription  
> > aliases:   
> > block\_params:  
> > comment:  
> > &nbsp;&nbsp;&nbsp;- !ruby/struct:SM::Flow:😛  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;body: "Exclusive Or---If \<em\>obj\</em\> is \<tt\>nil\</tt\> or  
> > \<tt\>false\</tt\>, returns \<tt\>false\</tt\>; otherwise, returns \<tt\>true\</tt\>."  
> > full\_name: FalseClass#^  
> > is\_singleton: false  
> > name: "^"  
> > params: \>  
> > &nbsp;&nbsp;&nbsp;false ^ obj =\> true or false
> > 
> > &nbsp;&nbsp;&nbsp;nil ^ obj =\> true or false
> > 
> > visibility: public
