# With YAML can I retrieve the class type back?

**URL:** <https://www.ruby-forum.com/t/with-yaml-can-i-retrieve-the-class-type-back/162047>\
**Category:** Ruby\
**Created:** [March 5, 2009, 8:15am UTC](https://www.ruby-forum.com/t/with-yaml-can-i-retrieve-the-class-type-back/162047 "2009-03-05T08:15:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [March 5, 2009, 8:15am UTC](https://www.ruby-forum.com/t/with-yaml-can-i-retrieve-the-class-type-back/162047/1 "2009-03-05T08:15:59Z")

</div>

Hi,  
If I wanted to persist different Ruby types (e.g. float, BigDecimal,  
integer, string) to a database based table (e.g. column names KEY and  
VALUE), if I use the “to\_yaml” method to serialize their values and  
store  
them as strings, what’s the easiest way to retrieve them back to a Ruby  
variable such that they are of the original type (i.e. not a YAML type).

E.g. Want to be able to do things such as (assume I have a model called  
Config):

Config.create!(:key =\> “interest\_rate”, :value =\>  
BigDecimal(‘12.12’).to\_yaml)  
Config.create!(:key =\> “age”, :value =\> 12.to\_yaml)

and then later

ir = Config.find\_by\_key('interest\_rate") # and the variable should be  
of  
type BigDecimal  
ir = Config.find\_by\_key('age") # and the variable should be of type  
Integer  
etc

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [March 5, 2009, 8:41am UTC](https://www.ruby-forum.com/t/with-yaml-can-i-retrieve-the-class-type-back/162047/2 "2009-03-05T08:41:11Z")

</div>

Greg H. wrote:

> Hi,  
> If I wanted to persist different Ruby types (e.g. float, BigDecimal,  
> integer, string) to a database based table (e.g. column names KEY and  
> VALUE), if I use the “to\_yaml” method to serialize their values and store  
> them as strings, what’s the easiest way to retrieve them back to a Ruby  
> variable such that they are of the original type (i.e. not a YAML type).

If you’re using Rails, there’s a helper built in. For example, if your  
column named “value” is the object you want stored that way:

class Whatever \< ActiveRecord::Base  
serialize :value  
…  
end

If you want to do it yourself, use YAML.load:

serialized = my\_object.to\_yaml  
object = YAML.load(serialized)

I have no idea if this is safe for untrusted data – I suspect it’s not.  
So, use this with your own databases, not with random web services, at  
least without putting some thought into it.

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [March 5, 2009, 10:29am UTC](https://www.ruby-forum.com/t/with-yaml-can-i-retrieve-the-class-type-back/162047/3 "2009-03-05T10:29:09Z")

</div>

2009/3/5 David M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> If you’re using Rails, there’s a helper built in. For example, if your  
> column named “value” is the object you want stored that way:
> 
> class Whatever \< ActiveRecord::Base  
> serialize :value  
> …  
> end
> 
> If you want to do it yourself, use YAML.load:

thanks David - I hadn’t realized this was there
