1

I have an array of hashes returned from the below map function. I'm trying to populate data for a graph (I'm using AM Charts), which takes data in a specific format mentioned below-

Sample structure of a company record:

1.9.3p327 :073 > Company
 => Company(id: integer, name: string, contact_no: integer, email_id: string, website: string, fax_no: integer, created_at: datetime, updated_at: datetime, divisions_count: integer) 

@all_companies = Company.all

Map function:

@all_companies = @all_companies.map {|each_company| { Country: each_company.name, Visits: each_company.divisions_count} }

Output format of above query:

[{:country=>"Samsung", :visits=>8}, {:country=>"Natraj", :visits=>2}, {:country=>"Tupperware", :visits=>5}, {:country=>"Transcen", :visits=>0}, {:country=>"camlin", :visits=>0}]

Graph data input format:

  var chartData = [{
      country: "USA",
      visits: 4025
  }, {
      country: "China",
      visits: 1882
  }, {
      country: "Japan",
      visits: 1809
  }, {
      country: "Germany",
      visits: 1322
  }]

 

I'm using Ruby 1.9.3-p327. I need to convert my output array to the specific graph format. I try going about the same through the following steps:

  1.9.3p327 :070 >     @all_companies = @all_companies.to_s.gsub(":","")
 => "[{country=>\"Samsung\", visits=>8}, {country=>\"Natraj\", visits=>2}, {country=>\"Tupperware\", visits=>5}, {country=>\"Transcen\", visits=>0}, {country=>\"camlin\", visits=>0}]" 
1.9.3p327 :071 >     @all_companies = @all_companies.gsub("=>",": ")
 => "[{country: \"Samsung\", visits: 8}, {country: \"Natraj\", visits: 2}, {country: \"Tupperware\", visits: 5}, {country: \"Transcen\", visits: 0}, {country: \"camlin\", visits: 0}]" 
1.9.3p327 :072 > puts @all_companies
[{country: "Samsung", visits: 8}, {country: "Natraj", visits: 2}, {country: "Tupperware", visits: 5}, {country: "Transcen", visits: 0}, {country: "camlin", visits: 0}]
 => nil 
1.9.3p327 :073 > 

Now when I'm trying to display the data in my graph, I'm getting the below syntax error:

var chartData = [{country: "Samsung", visits: 8}, {country: "Natraj", visits: 2}, {country: "Tupperware", visits: 5}, {country: "Transcen", visits: 0}, {country: "camlin", visits: 0}]

The syntax error from the Firebug console now points to &quot in the above data.

Any work-arounds that could get me through the last step so that I can get the desired graph format?

Question updated

I need the data in following format:

  var chartData = [

      {
          country: "Samsung",
          visits: 8
      },
      {
          country: "Natraj",
          visits: 2
      },
      {
          country: "Tupperware",
          visits: 5
      },
      {
          country: "Transcen",
          visits: 0
      },
      {
          country: "camlin",
          visits: 0
      }

      ]
6
  • Please add correct final output.
    – d.danailov
    Sep 3, 2013 at 4:39
  • d.danailov - Question updated, it now shows the desired correct final output. Sep 3, 2013 at 5:23
  • 1
    May I know why is the question downvoted and there's an upvote to close this question ? Sep 3, 2013 at 5:27
  • Dear All, I've updated my question and the question title. Hope the question is more clear now. If so, I would appreciate if you could undo your close vote option. Sep 3, 2013 at 6:03
  • Just rewrite your question. Show what you have and what you want. Nothing else.
    – oldergod
    Sep 3, 2013 at 7:10

3 Answers 3

1

Because you are not printing your value. The console is only displaying it, double quotes are escaped.
Otherwise how would you know where my string: "my "string" what" ends?

If you just do

puts @specific_details.to_s

you will not see those double quotes escaped.

2
  • oldergod - I get your point now. Thanks. I've updated my question and the question title. Kindly check. Sep 3, 2013 at 5:52
  • oldergod - +1 for pointing out that the backslashes in "[{country: \"Samsung\", visits: 8}, {country: \"Natraj\", visits: 2}, {country: \"Tupperware\", visits: 5}, {country: \"Transcen\", visits: 0}, {country: \"camlin\", visits: 0}]" needn't have to be separately removed. Thank you. Sep 3, 2013 at 7:25
1

I need to convert my output array to the specific graph format.

Do this using Awesome Print Rubygem -

require "awesome_print"

hsh = [{:Country=>"Samsung", :Visits=>8}, {:Country=>"Natraj", :Visits=>2}, 
       {:Country=>"Tupperware", :Visits=>5}, {:Country=>"Transcen", :Visits=>0}, 
      {:Country=>"camlin", :Visits=>0}]

ap hsh,{:index => false}

output

[
    {
        :Country => "Samsung",
         :Visits => 8
    },
    {
        :Country => "Natraj",
         :Visits => 2
    },
    {
        :Country => "Tupperware",
         :Visits => 5
    },
    {
        :Country => "Transcen",
         :Visits => 0
    },
    {
        :Country => "camlin",
         :Visits => 0
    }
]
4
  • Country, Visits shouldn't be in quotes. Sep 3, 2013 at 5:09
  • @boddhisattva see now!! Sep 3, 2013 at 5:31
  • babai - Thanks for your suggestions. I'm sorry, thanks to oldergod I figured out that the backslash is not there anymore when I do a puts @all_companies from my console. Please see my updated question title and the updated question wrt the current issue I'm facing in getting input data for the graph in the appropriate format. Please bare with me for any inconvenience Sep 3, 2013 at 6:01
  • +1 for introducing me to the pretty_generate and for awesome print gem. I didn't know something like pretty_generate existed earlier. I think it would be quite useful going forward. Thanks. Sep 3, 2013 at 7:23
0

I figured it out thanks to this question.

This is what I do to overcome the &quot syntax error.

In the controller:-

def company_division_stats
    @all_companies = @companies_data = Company.all 

    @specific_details = @all_companies = @all_companies.map {|each_company| { country: each_company.name, visits: each_company.divisions_count} } 
    #@all_companies = @all_companies.to_s.gsub(":","")
    #@all_companies = @all_companies.gsub("=>",": ")

    respond_to do |format|
      unless @all_companies.nil?
        format.html
        format.json { render json: @specific_details, status: :created } #in the browser url would be:- localhost:3000/company_division_stats.json
      end
    end

In the view:-

var chartData = <%= @all_companies.to_json.html_safe %>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.