Q:

Student Marks Hash

I've seen solutions posted in other languages but not Ruby so I'm asking here.
I'm trying to create a student marks system using Ruby, this should take the student’s name and two marks from the user for that particular student.
I decided to try and store these in a hash so it would end up looking something like:
    student_marks = {
        "Steve" => 45, 65,
        "James" => 20, 75,
        "Scott" => 30, 90
    }

My code attempt is as follows:
    continue = "y"

    student_grades = Hash.new

    while continue == "y"

    puts "Please enter student name"
    name = gets.chomp

    puts "Please enter the first grade for #{name}"
    grade_one = gets.chomp.to_i

    puts "Please enter the second grade for #{name}"
    grade_two = gets.chomp.to_i

        student_grades.each do |key, value|
            student_grades[key] = name
            student_grades[value] = grade_one
            student_grades[value][1] = grade_two

         end

        puts "Do you want to continue? y or n"
        continue = gets.chomp

    end

    puts student_grades

Obviously there is a problem with my logic in trying to populate the hash using each/iteration because I keep getting nil return. I guess I could use arrays and populate the hash from them, but is there a way to populate the hash both the keys and values using iteration from user input?

A:

You seem to be misunderstanding how hashes work in Ruby. I would suggest reading up on it a bit.
In the meantime, try this:
student_grades = Hash.new {|h,k| h[k] = [] }

Now every time you get a student grade, you can push it into the array value for that student's hash key.
For example:
student_grades
=> {}

student_grades['Mark'] << 95
student_grades['Mark'] << 86

student_grades
=> {'Mark' => [95, 86]}

You don't need to iterate over the hash at all in each run of your loop, you should only be doing that if you need to extract some information from it.

