What is a good way to convert some common Ruby objects (like strings, hashes, arrays) into corresponding Javascript objects? For example, jQuery css()
accepts a hash as an argument. Suppose I have a Ruby hash like this:
h = {"background-color" => "yellow", "color" => "green"}
I want to embed this ruby object into a string so that it becomes a valid javascript (jQuery) command. My best attempt now is to convert it via json like this:
"$('#foo').css(JSON.parse('#{h.to_json}'));"
but it is not working well. I want a more direct and working way to do it. Is there a good way?