Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8640313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:14:55+00:00 2026-06-12T11:14:55+00:00

How do i declare a 2D array of type string and int? I want

  • 0

How do i declare a 2D array of type string and int?

I want to do something like this

@products.each do |p|
   array = [p.title, p.price]
end

But i get an error saying cannot convert string to int

The problem is i can only call f.series() once and i need an array to hold all of my data

f.series(:name => 'Product Sales', :data => array)

I’m trying to follow this code to create a pie chart

https://github.com/bakongo/highcharts_plugin_sample_graphs/blob/master/app/controllers/graphs_controller.rb

def pie_chart
@categories = generate_categories(6)
@numbers = generate_numbers(6)
assoc = []
@categories.each_with_index {|c,i| assoc << [c, @numbers[i]]}

@highchart = HighChart.new('graph') do |f|
  f.title(:text => 'Flowers in Yard')
  f.options[:chart][:defaultSeriesType] = "pie"
  f.options[:x_axis][:categories] = @categories
  f.series(:type => 'pie', :name => 'Flower Presence', :data => assoc)
end

def generate_numbers(number)
   numbers = [rand(number)]
   (1...number).each_with_index {|v, i| numbers << (rand(number)+1)}
   numbers
end

def generate_categories(number)
   cats = ['Sunflower', 'Magnolia', "Rose", 'Lily', 'Tulip', 'Iris']
   cats[0...number]
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T11:14:57+00:00Added an answer on June 12, 2026 at 11:14 am

    In ruby you can save different type of the element in the single array like following

    arr = [1, 1.0, "This is a String", {abc: pqr}, [6]]
    #arr[0].class = Fixnum
    #arr[1].class = Float
    #arr[2].class = String
    #arr[3].class = Hash
    #arr[4].class = Array
    

    So if you want to save array of array like following

    [["value1", 1.0], ["value2", 2.0]]
    

    Use following

    array = []
    @products.each do |p|
      array << [p.title, p.price]
    end
    

    OR just

    array =  @products.collect{|p|  [p.title, p.price]}
    

    Edited to show the access the 2D Array

    arr = [["value1", 1.0], ["value2", 2.0]]
    #arr[0] = ["value1", 1.0] 
    #arr[0][0] = "value1" 
    #arr[0][1] = 1.0
    #arr[1] = ["value2", 2.0]
    #arr[1][0] = "value2" 
    #arr[1][1] = 2.0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know I can declare an array of string in XAML like this: <x:Array
I'm trying to call method from .jar But when I want to declare array
I need to declare 3d array variable but can't. int[][][] ary = new int[5][2][];
suppose I declare a dynamic array like int *dynArray = new int [1]; which
I declare array like that private double[] array = new double[length] . Is it
I want to declare an array of handel as the following code: using namespace
I am trying to declare an array of pointers each of which points to
<script type=text/javascript> function bindCity() { // Some javascript code //declare options array and populate
Can I declare an int array, then initialize it with chars? I'm trying to
I can declare an array of maps using generics to specify the map type:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.