在数组中访问相同的哈希标题

时间:2014-07-05 22:34:49

标签: ruby arrays hash

所以我有一个内部有哈希的数组。我想访问数组所有部分中的points键。反正有没有这样做?

@students = []
loop do
  puts "What is the student's name?"  
  @name = gets.chomp 
  puts "What is the students GPA?"
  @gpa = gets.chomp.to_f 

  def add_students 
    @students << {:name => @name, :gpa => @gpa, :points => @gpa * 10} 
  end 

  add_students
  puts @students 


  loop do
     puts "Would you like to add more students?"
     answer = gets.chomp
     if answer == "yes"
        break 
     elsif answer == "no"
        `say "goodbye"`
            break 
            break 
     else 
        puts "I'm sorry this is not a correct command!"
    end
 end 
end 

1 个答案:

答案 0 :(得分:2)

您可以使用...

生成所有点值的数组
points_array = @students.map{|s| s[:points] }