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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:34:13+00:00 2026-06-11T11:34:13+00:00

Arrays have always been my downfall in every language I’ve worked with, but I’m

  • 0

Arrays have always been my downfall in every language I’ve worked with, but I’m in a situation where I really need to create a dynamic array of multiple items in Rails (note – none of these are related to a model).

Briefly, each element of the array should hold 3 values – a word, it’s language, and a translation into English. For example, here’s what I’d like to do:

myArray = Array.new

And then I’d like to push some values to the array (note – the actual content is taken from elsewhere – although not a model – and will need to be added via a loop, rather than hard coded as it is here):

myArray[0] = [["bonjour"], ["French"], ["hello"]]

myArray[1] = [["goddag"], ["Danish"], ["good day"]]

myArray[2] = [["Stuhl"], ["German"], ["chair"]]

I would like to create a loop to list each of the items on a single line, something like this:

<ul>
<li>bonjour is French for hello</li>
<li>goddag is Danish for good day</li>
<li>Stuhl is German for chair</li>
</ul>

However, I’m struggling to (a) work out how to push multiple values to a single array element and (b) how I would loop through and display the results.

Unfortunately, I’m not getting very far at all. I can’t seem to work out how to push multiple values to a single array element (what normally happens is that the [] brackets get included in the output, which I obviously don’t want – so it’s possibly a notation error).

Should I be using a hash instead?

At the moment, I have three separate arrays, which is what I’ve always done, but I don’t particularly like – that is, one array to hold the original word, one array to hold the language, and a final array to hold the translation. While it works, I’m sure this is a better approach – if I could work it out!

Thanks!

  • 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-11T11:34:14+00:00Added an answer on June 11, 2026 at 11:34 am

    Ok, let’s say you have the words you’d like in a CSV file:

    # words.csv
    bonjour,French,hello
    goddag,Danish,good day
    stuhl,German,chair
    

    Now in our program we can do the following:

    words = []
    File.open('words.csv').each do |line|
      # chomp removes the newline at the end of the line
      # split(',') will split the line on commas and return an array of the values
      # We then push the array of values onto our words array
      words.push(line.chomp.split(','))
    end
    

    After this code is executed, the words array had three items in it, each item is an array that is based off of our file.

    words[0]  # => ["bonjour", "French", "hello"]
    words[1]  # => ["goddag", "Danish", "good day"]
    words[2]  # => ["stuhl", "German", "chair"]
    

    Now we want to display these items.

    puts "<ul>"
    words.each do |word|
      # word is an array, word[0], word[1] and word[2] are available
      puts "<li>#{word[0]} is #{word[1]} for #{word[2]}</li>"
    end
    puts "</ul>"
    

    This gives the following output:

    <ul>
    <li>bonjour is French for hello</li>
    <li>goddag is Danish for good day</li>
    <li>stuhl is German for chair</li>
    </ul>
    

    Also, you didn’t ask about it, but you can access part of a given array by using the following:

    words[0][1]  # => "French"
    

    This is telling ruby that you want to look at the first (Ruby arrays are zero based) element of the words array. Ruby finds that element ([“bonjour”, “French”, “hello”]) and sees that it’s also an array. You then asked for the second item ([1]) of that array and Ruby returns the string “French”.

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

Sidebar

Related Questions

Possible duplicate : comparing-two-arrays I have two NSArray and I'd like to create a
I have always been displaying such things in the main thread and using events
I've always been hesitant to learn JS since I have to go out of
I am a dummy in Fortran 77 and have always been a C++ coder,
Up until now, I have always been using JSP to display pages. When a
I have a 2-dimensional jagged array (though it's always rectangular), which I initialize using
Arrays have a "length" property by default. Can I add custom properties to them?
I have a struct which has several arrays within it. The arrays have type
I have arrays like this. [11] => Array ( [transactioncurrencyid] => Array ( [!name]
guys i have arrays in which i have to match this kind of text

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.