Ruby: How to Add an Array to Another Array

To add elements to an array in Ruby, there are several options. One option is to use the Concat documentation to concatenate arrays. Another option is to use the Array stuffs feature to prepend or append arrays. The Ruby docs link provides additional information on using slice and other array functions. In regards to adding arrays to arrays, the user can enter an array name, add values to it, and even add additional arrays and values if desired.


Question:

My aim is to acquire the skill of converting
add arrays
to arrays. For this purpose, I have written the subsequent code:

puts "would you like to save a data set"
response = gets.chomp
if response == "y"
  puts "create a new dataset?"
  create_data_set = gets.chomp
  while create_data_set == "y"
    puts "what do you want to name the data set?"
    dataset = gets.chomp
    dataset = Array.new
    puts 'would you like to add some grades to the array?'
    store_grades_response = gets.chomp
    while store_grades_response == "y"
        puts 'enter grade ->'
        grade = gets.chomp.to_i
        dataset << grade
        puts 'would you like to store another grade?'
        store_grades_response = gets.chomp
    end
    all_data_sets = Array.new
    all_data_sets.push(dataset)
    puts "would you like to create a new data set?"
    create_data_set = gets.chomp
  end
end
puts all_data_sets

The user is prompted to provide an array name, which will be used to create an array, referred to as
add values
. The user has the option to add additional arrays and values to this array. Once all the arrays and values have been added, they are grouped into one final array. Finally, all the arrays are displayed for the user to view.

The program is functioning properly as I am able to iterate through all elements. However, I am encountering an issue where only the final array created is displayed. Is there a way to store all arrays in a single array called

all_data_sets

?


Solution 1:

The issue arises from the creation of a new array

all_data_sets

after each loop. A possible resolution would be to have the array initialized before the loop.

puts "would you like to save a data set"
response = gets.chomp
all_data_sets = []
if response == "y"
  puts "create a new dataset?"
  create_data_set = gets.chomp
  while create_data_set == "y"
    puts "what do you want to name the data set?"
    dataset = gets.chomp
    dataset = Array.new
    puts 'would you like to add some grades to the array?'
    store_grades_response = gets.chomp
    while store_grades_response == "y"
        puts 'enter grade ->'
        grade = gets.chomp.to_i
        dataset << grade
        puts 'would you like to store another grade?'
        store_grades_response = gets.chomp
    end
    all_data_sets << dataset
    puts "would you like to create a new data set?"
    create_data_set = gets.chomp
  end
 end
puts all_data_sets

By repeating the loop, the

datasets

is continuously directed towards the

all_data_sets

.

I hope this is explanatory enough.


Solution 2:

Fix

To avoid constantly creating a new_data_sets array with each loop, it’s recommended to declare it outside of the enclosing while loop.

Code

def main
    mainDataSet = [] # All datasets
    dataSetNames = [] # Incase you want to store data set names 
    response = getInput("Would you like to save a data set")
    if(response == "y")
        choice = getInput("Create a new dataset?")          
        while choice == "y"
            dataset = getInput("What do you want to name the data set?")
            dataSetNames << dataset
            dataset = []
            choice_2 = getInput("would you like to add some grades to the array?")
            while choice_2== "y"
                grade = getInput("Enter grade")
                dataset << grade
                choice_2 = getInput("Store another grade?")
            end
            mainDataSet << dataset
            choice = getInput("Create a new data set?")
        end
    end
    puts mainDataSet
    puts dataSetNames
end
def getInput(message)
    puts "#{message} -> "
    gets.chomp
end

Hope this helps.


Solution 3:


You have the option to concatenate, prepend or perform
append arrays
in a similar manner.

dataset.concat all_dataset
dataset + all_dataset

Concat documentation

Add to the beginning or end.

dataset.push(*all_dataset)
all_dataset.unshift(*dataset)

Array stuffs

Additionally, the Ruby docs link provides information on how to perform slicing and other related actions.

Frequently Asked Questions