-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclasses.rb
More file actions
41 lines (31 loc) · 721 Bytes
/
classes.rb
File metadata and controls
41 lines (31 loc) · 721 Bytes
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
#!/usr/bin/env ruby
class Message
@@num_messages_sent = 0
def initialize from, to
@from = from
@to = to
@@num_messages_sent += 1
end
def to_s
"From: #{@from}; To: #{@to}"
end
def Message.num_messages_sent
@@num_messages_sent
end
end
class Email < Message
attr_accessor :subject, :body
def initialize from, to, subject, body=""
super from, to
@subject = subject
end
def to_s
super + "; Subject: #{@subject}; Body: #{@body}"
end
end
my_message = Message.new "Sauron", "Saruman"
puts my_message
my_email = Email.new "Gandalf", "Galadriel", "Saruman"
my_email.body = "We may have a problem."
puts my_email
puts "Messages sent: #{Message.num_messages_sent}"