-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclassy.rb
57 lines (46 loc) · 1.38 KB
/
classy.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
43
44
45
46
47
48
49
50
51
52
53
54
55
# start of the definition of the template for a Person
class Person
attr_accessor :first_name, :last_name
def initialize(first_name, last_name)
@first_name = first_name
@last_name = last_name
end
def full_name
return @last_name + ", " + @first_name
end
def say_something
puts "hello classy! My name is " + @first_name
end
end
# end of the template definition for a Person
# start of the template definition for a Teacher
# Teacher inherits characteristics (attributes and methods) from Person
class Teacher < Person
def teach
puts "Let's learn to code!"
end
end
#
# code starts executing here...
# class and method definitions aren't executed unless called (methods)
# or 'instantiated' (fancy way of saying you create an object based on a class, like var = Teacher.new)
prof = Teacher.new("Paula","Paul")
prof.say_something
prof.teach
paula = Person.new("Paula", "Paul")
puts paula.full_name + " says... "
paula.say_something
puts " " # just some blank space between puts'
people = []
people.push Person.new("John", "Doe")
people.push Person.new("Mary", "Mac")
people.push Person.new("Tommy", "Tunes")
people[0].say_something
people[1].say_something
people[2].say_something
#gee, what if there was an easy way to get
#all these people to say something, no matter how many?
puts " " # make some space
people.each do |person|
person.say_something
end