-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise4-1.rb
42 lines (36 loc) · 1.58 KB
/
exercise4-1.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class Person
attr_writer :name
def initialize (input_name = 'John Doe')
@name = input_name
end
def greeting
puts "Hi my name is #{@name}!"
end
end
class Instructor < Person
def teach
puts 'Everything in Ruby is an Object'
end
end
class Student < Person
# @return [Puts Statement] Returns nothing; just puts object.
def learn
puts 'I get it!'
end
end
instructor = Instructor.new('Chris')
instructor.greeting
student = Student.new('Cristina')
student.greeting
instructor.teach
student.teach # This is expected not to return as the Student class does not define teach.
=begin
Let's start by creating two classes: one called Student and another called Instructor.
The student class has a method called learn that outputs "I get it!".
The instructor class has a method called teach that outputs "Everything in Ruby is an Object".
Both the instructor and the student have names. We know that instructors and students are both people. Create a parent Person class that contains the attribute name and an initializer to set the name.
Both the instructor and the student should also be able to do a greeting, like "Hi, my name is #{name}". Where's the best place to put this common method?
Create an instance of Instructor whose name is "Chris" and call his greeting.
Create an instance of Student whose name is "Cristina" and call her greeting.
Call the teach method on your instructor instance and call the learn method on your student. Next, call the teach method on your student instance. What happens? Why doesn't that work? Leave a comment in your program explaining why.
=end