In this repo, there's four ruby files. Here's what's in each of them:
Person.rb
- This contains the rootPerson
class. ThePerson
class accepts in a name and an age, and then defines ato_s
method that prints the name and the age.Student.rb
- This contains theStudent
class. TheStudent
class inherits all methods from thePerson
class (meaning it also has a name and an age, as well as ato_s
method). It also accepts in ateacher
variable in theinitialize
method, which is expected to be aTeacher
object. It stores theteacher
variable in a instance variable, and then runs theadd_student
method onteacher
. It also has aprint_classmates
method that runsprint_students
onteacher
.Teacher.rb
- This contains theTeacher
class. It inherits fromPerson
as well, meaning it inherits all of the methods and instance variables from thePerson
class. It also sets up astudents
instance variable that's an array of all the teacher's students. Theadd_student
method adds a student to this array. Theprint_students
method prints all of the teacher's students.main.rb
- This contains some test code that creates aTeacher
object and gives them a bunch ofStudent
s.
- Check out the
main.rb
file. Try to figure out how it works (don't worry about the weird code with thename
variable). - Try running the
print_students
method on theprofessor
variable. See what that outputs. - Create a
delete_students
method on theTeacher
class that deletes all students in thestudents
array (hint: you can just set thestudents
variable equal to an empty array). - Create a
students_names
method on theTeacher
class that returns an array of all of the students' names. - Create a
reverse_name
on the Person class that returns thename
variable reversed (HINT: use the.reverse!
method on the string). Modify yourstudent_names
method so that it returnsreverse_name
instead of name.