将数组添加到数组Ruby

时间:2016-10-09 13:04:44

标签: arrays ruby

我试图学习如何将数组添加到数组中,我有以下代码:

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

我基本上要求用户输入一个数组名称,该名称应该创建一个数组,向数组添加值,如果用户需要,还要为它添加更多的数组和值。最后,应该将数组添加到数组中。然后我试图显示所有阵列。

代码工作正常,我循环遍历所有内容但是puts all_data_sets它只显示最后创建的数组?我想将所有数组存储在一个名为all_data_sets

的数组中

3 个答案:

答案 0 :(得分:1)

问题是您在每个循环结束时创建一个新数组all_data_sets。一种解决方案是在循环之前使用它。

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

这样,您可以在每次循环后继续将datasets推入all_data_sets

我希望这足够说明。

答案 1 :(得分:0)

修复

因为你每次进行循环时都创建new_data_sets数组,所以将它声明为封闭while循环

代码

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

希望这有帮助。

答案 2 :(得分:-1)

你可以像这样连接,前置或附加数组

dataset.concat all_dataset
dataset + all_dataset

Concat documentation

前置或附加

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

Array stuffs

你也可以在ruby docs链接上查看切片和一大堆东西