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

  • Home
  • SEARCH
  • 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 515065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:35:23+00:00 2026-05-13T07:35:23+00:00

Is there a way to dynamically create arrays in Ruby? For example, let’s say

  • 0

Is there a way to dynamically create arrays in Ruby? For example, let’s say I wanted to loop through an array of books as input by a user:

books = gets.chomp

The user inputs:

"The Great Gatsby, Crime and Punishment, Dracula, Fahrenheit 451,
Pride and Prejudice, Sense and Sensibility, Slaughterhouse-Five, 
The Adventures of Huckleberry Finn"

I turn this into an array:

books_array = books.split(", ")

Now, for each book the user input, I’d like to Ruby to create an array. Pseudo-code to do that:

 x = 0

 books_array.count.times do
    x += 1
    puts "Please input weekly sales of #{books_array[x]} separated by a comma."
    weekly_sales = gets.chomp.split(",")
 end

Obviously this doesn’t work. It would just re-define weekly_sales over and over again. Is there a way to achieve what I’m after, and with each loop of the .times method create a new array?

  • 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-13T07:35:23+00:00Added an answer on May 13, 2026 at 7:35 am
    weekly_sales = {}
    puts 'Please enter a list of books'
    book_list = gets.chomp
    books = book_list.split(',')
    
    books.each do |book|
      puts "Please input weekly sales of #{book} separated by a comma."
      weekly_sales[book] = gets.chomp.split(',') 
    end
    

    In ruby, there is a concept of a hash, which is a key/value pair. In this case, weekly_sales is the hash, we are using the book name as the key, and the array as the value.

    A small change I made to your code is instead of doing books.count.times to define the loop and then dereference array elements with the counter, each is a much nicer way to iterate through a collection.

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

Sidebar

Ask A Question

Stats

  • Questions 509k
  • Answers 509k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Yes, you can sir! // Get the thumbnail $thumb =… May 16, 2026 at 4:33 pm
  • Editorial Team
    Editorial Team added an answer Looks like you are using zoom (i can see something… May 16, 2026 at 4:33 pm
  • Editorial Team
    Editorial Team added an answer That depends entirely on what these categories are supposed to… May 16, 2026 at 4:33 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Let's say I have a PHP Model-View-Controller framework that maps an address like http://example.com/admin/posts/edit/5
I'm trying to build an associative array in PHP dynamically, and not quite getting
I am trying to dynamically filter search results. Every result has tags associated with
This is my little big question about containers, in particular, arrays. I am writing
I try to add dynamically an iframe to a web page using JavaScript. I
I have a table that I am trying to dynamically delete rows from. Each
I have got a problem with my codeigniter library. To transform database data into
I am working on a top-down view 2d game at the moment and I
I have a panel sitting in a div, and I want to use that
I am creating a web application that acts as a priority list, allowing a

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.