tags:

views:

107

answers:

3

I'm working with some serialized data from a MySQL database and I need to deserialize this using Ruby (the serialized data is used to build up a WHERE clause for a database query). PHP has the unserialize() method which will convert it into an Array; what is the Ruby equivalent of this?

The data in question looks like this, if it helps any:

a:2:{s:5:"Lists";a:1:{i:0;s:2:"11";}s:5:"Rules";a:1:{i:0;a:3:{s:4:"type";s:5:"group";s:9:"connector";s:3:"and";s:5:"rules";a:1:{i:0;a:3:{s:4:"type";s:4:"rule";s:9:"connector";s:3:"and";s:5:"rules";a:3:{s:8:"ruleName";s:2:"21";s:12:"ruleOperator";s:10:"isnotempty";s:10:"ruleValues";a:1:{i:0;s:0:"";}}}}}}}
A: 

Look at Ruby's Marshal Class.

From the docs:

The marshaling library converts collections of Ruby objects into a byte stream, allowing them to be stored outside the currently active script. This data may subsequently be read and the original objects reconstituted. Marshaled data has major and minor version numbers stored along with the object information.

Of course this is a two way street, you can only un-Marshal, Marshaled ruby objects.

Mark
+2  A: 

I guess the exact equivalent would be this. You could also check out Ruby's Marshall Class, specifically Marshal.load.

Nick Presta
That looks like just what I need. Thanks!
Wayne M
A: 

If it's XML, there's the Hash.from_xml method.

JRL
It's not, sadly.
Wayne M