Skip to content

Instantly share code, notes, and snippets.

@vanderhoop
Last active August 29, 2015 14:11
Show Gist options
  • Select an option

  • Save vanderhoop/71814c9720869b92b606 to your computer and use it in GitHub Desktop.

Select an option

Save vanderhoop/71814c9720869b92b606 to your computer and use it in GitHub Desktop.

Dissecting the student class

You just learned how to write a class definition with an initialize method. But it takes time for some concepts to sink in. Tats why you're going to partner up and explain your understanding in your own words.

Directions

  1. Partner up.
  2. Examine the code below with your partner.
  3. With your partner, answer the questions that follow.

The Student class definition

class Student
  def initialize(f_name, l_name, occupation)
    @first_name       = f_name
    @last_name        = l_name
    @prior_occupation = occupation
  end
  
  def first_name=(x)
    @first_name = x
  end
  
  def first_name
    return @first_name
  end
end

student_one = Student.new("Chadd", "Clairmont", "Scuba Diving Instructor")

student_two = Student.new("Kristen", "Macfarlane", "Academic")

Questions to Answer with Your Partner

In the code above:

  1. What is class ?
  2. What is Student ?
  3. What type of method is first_name= ?
  4. What type of method is first_name ?
  5. What type of variable is @first_name ?
  6. What does the initialize method do?
  7. When does the initialize method execute?
  8. What's the difference between a class and an instance?
  9. What type of object does the variable student_one hold?
  10. What type of object does the variable student_two hold?
  11. What will the return value of student_one.first_name be?
  12. What will the return value of student_two.first_name be?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment