1

Is there any pure ruby implementation of YAML? I need to do following (in Rhomobile):

YAML.load( hash_string.to_yaml )

where the hash_string is a string representation of a hash. I can use neither the standard Ruby YAML library (in Rhomobile framework) nor the eval function (as it is still not implemented).

I have come across some incomplete implementations of YAML in pure Ruby (e.g., ya2yaml). However, load and to_yaml together or the variants of them are probably not supported in the same library.

Why am I asking this:

Only string data type is supported in Rhom database. A hash attribute of a ruby object gets converted to a string when saved in the database. I would like to get the original hash back (i.e., or convert it from the string) when I retrieve the object from the database. I am open to any other suggestions. I would appreciate if you can provide me with step by step instructions to add the standard Ruby YAML implementation as an extension to Rhomobile framework. I have read the instructions given here. However, I don't know the entry point of the SYCK library that I need to put in ext.yml.

I think regular expression can also be used for this purpose. I am not sure how reliable it would be.

My sincere gratitude to you all.

EDIT: Following worked for me.

Rho::JSON.parse( hash_string.gsub("=>", ":") )
rayhan
  • 197
  • 1
  • 6
  • 1
    Have you considered JSON instead of YAML? JSON is a bit more robust IMHO. – mu is too short Apr 03 '11 at 08:18
  • @mu-is-too-short Initially, I parsed a JSON string using Rho::JSON.parse (string), which gave me a hash (actually a hash of hashes). I then create a ruby object from this hash by passing it to the constructor. Now, when I store this in the database, some of the attributes which themselves are hashes get converted to string. When I retrieve the object from the database I get those strings back instead of the hashes. However, I tried to do it using JSON but it couldn't parse it as the string representation is not a valid JSON string. Now that you have told me this, I am going to experiment .... – rayhan Apr 03 '11 at 08:29
  • @mu-is-too-short I am planning to replace the "=>" symbol with the ":" symbol in the string and try to parse it with JSON. If everything works out, I am going to accept your answer (I am not sure if you can do that for a comment). – rayhan Apr 03 '11 at 08:30

0 Answers0