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 7001539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:48:59+00:00 2026-05-27T20:48:59+00:00

I have a multi-dimensional array called my_ids_and_names It should probably be a hash but

  • 0

I have a multi-dimensional array called my_ids_and_names
It should probably be a hash but lets leave it as an array for now 🙂

I create it with: my_own_array = Array.new[10],[2]

I have a variable @my_ids which has the id’s that I want.

I populate it within a method like this:

# Setup
@my_ids_and_names = Array.new[10],[2]
@my_ids.each do |cid|
  @my_ids_and_names[cid][1] = my_id
  @my_ids_and_names[cid][2] = MyModel.find(my_id).internal_name
end
@my_ids_and_names

Now I want to retrieve information in views.
I am trying:

<% @campaign_ids_and_names.each do |cid, nm| %>
  "various bits of code..."
  var_for_id = cid
  var_for_nm = nm
<% end %>

Will the cid and nm be set correctly?

I want the loop to be able to spit out the cid/names as in

id  name
4   brick
9   tile
45  grout

Right now I am getting The error occurred while evaluating nil.[]= in the setup

  • 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-05-27T20:48:59+00:00Added an answer on May 27, 2026 at 8:48 pm

    If you fire up irb console, you’ll see that Array.new[10],[2] is not creating an array you wanted, instead it creates the following array:

    [nil, [2]]
    

    If you want to create a new array with 10 array elements, use the following constructor:

    Array.new(10, [])
    

    Update 1

    But as reading your question further I can see that you need a Hash instead of an Array, and you should keep in mind that ruby starts to index from 0 instead of 1.

    So:

    @my_ids_and_names = Hash.new
    @my_ids.each do |cid|
      @my_ids_and_names[cid] = Array.new
      @my_ids_and_names[cid][0] = my_id
      @my_ids_and_names[cid][1] = MyModel.find(my_id).internal_name
    end
    @my_ids_and_names
    

    Which can be further simplified to:

    @my_ids_and_names = {}
    @my_ids.each do |cid|
      @my_ids_and_names[cid] = [ my_id, MyModel.find(my_id).internal_name ]
    end
    @my_ids_and_names
    

    Update 2

    But that won’t work with your each loop what you’ve provided, so I would use the following code instead:

    @my_ids_and_names = {}
    @my_ids.each do |cid|
      @my_ids_and_names[cid] = MyModel.find(my_id).internal_name
    end
    @my_ids_and_names
    

    This will create a Hash with cid keys and the internal_name of MyModels as values.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a multi-dimensional array right now that looks like this: function art_appreciation_feeds() {
I have a multi dimensional array called soldier: Array ( [0] => Array (
I have a multi-dimensional array that I need to loop through and create <td></td>
I have this multi-dimensional array: char marr[][3] = {{abc},{def}}; Now if we encounter the
I have some code that creates a multi-dimensional array, but my result seems to
I have a multi-dimensional array, which basically consists of one sub-array for each year.
I have a multi-dimensional array, no problem. How do I interrogator one of the
I have a multi dimensional array. The only actual values (other than other arrays)
I have an multi-dimensional array that I want to send to a PHP script
I have a multi dimensional array, like this: array('name' => array('title'=>'Title','date'=>'Created')) I store it

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.