DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Ruby Object To JavaScript Object

12.17.2008
| 5184 views |
  • submit to reddit
        Convert a Ruby object to a JavaScript object.
Requires Rails and prototype.js

 javascript_object = "<%= 
   ruby_object.to_json.gsub('\\','\\\\\\\\').gsub('"', '\\"') 
%>".evalJSON()
    

Comments

Snippets Manager replied on Wed, 2008/07/23 - 8:43am

I don't get it. You convert a ruby object to JSON, and then back to ruby? Why not just this: json = ruby_object.to_json