I need to render a Ruby array in a very specific format for an erb template. This is because I am using JQcloud to build word clouds using JQuery, and is apparently picky about array formatting.
Here is the way that JQcloud requires a JS array to be formatted:
var word_array = [
{text: "Lorem", weight: 15},
{text: "Ipsum", weight: 9},
{text: "Dolor", weight: 6},
{text: "Sit", weight: 7},
{text: "Amet", weight: 5}
// ...as many words as you want
];
So what I have created in my controller are two arrays, one array of words to build my cloud, and another array of weights for each word.
ruby_word_array = ["Lorem","Ipsum","Dolor","Sit","Amet"]
ruby_weight_array = [15,10,13,8,4]
I need to convert my two Ruby arrays in such a way that they render properly with erb. So far I have come up with:
var wordArray = <%= raw @word_array.map{|a, i| {text: @word_array[0], weight: 15} } %> # I just hard coded the weight for simplicity
This gives me something that's close, but not close enough, and it ends up formatted like so:
var wordArray = [{:text=>"Lorem", :weight=>15}, {:text=>"Ipsum", :weight=>15}]
I really just need to know how to modify my last example to render in the Python style hash syntax {key: "value"},{key_2: "value_2"}
This is honestly a tricky one and I am stumped. The library I am using for this is called JQcloud, and it has a Ruby gem which I have incorporated into this Rails app. Here is a link.