已关闭,此问题需要details or clarity。它目前不接受回答。
**想改善这个问题吗?**通过editing this post添加详细信息并澄清问题。
4年前关闭。
Improve this question
我要求用户输入,并将其存储为数组中的散列。我需要从这些散列中提取共享相同键的值。我想打印属于同一队列的学生的姓名,如下所示:
"May cohort students are: Brian, Penelope"
"June cohort students are: Fred, Pedro"
我怎么能这么做?我需要这个方法的帮助:
def print(students)
#go through all hashes in 'students' and group them by cohort
#print each cohort separately
end
所以我可以用map
和select
来做:
def input_students
puts "Please enter the names of the students"
puts "To finish, just hit return twice"
students = []
name = gets.chomp
puts "In which cohort is this student?"
cohort = gets.chomp
while !name.empty? do
students << {cohort.to_sym => name}
name = gets.chomp
cohort = gets.chomp
end
students
end
students = input_students
print(students)
但我得到了:
"no implicit conversion of Symbol to Integer"
1条答案
按热度按时间bkkx9g8r1#
首先,我建议更改
input_students
,因为输入姓名和队列的建议只显示一次。用户无法理解输入的内容。我也建议修改这个方法的返回值。