-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcup.rb
More file actions
57 lines (46 loc) · 1.21 KB
/
cup.rb
File metadata and controls
57 lines (46 loc) · 1.21 KB
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
56
class Cup # Two words would be smooshed like CoffeeCup
# This makes our instance variable @drink_amount readable directly
attr_reader :drink_amount
def initialize
# Pretend like these amounts are percentages
@drink_amount = 0
puts "I'm alive!"
end
def fill
puts "Filling up!"
@drink_amount = 100
end
def empty
puts "Emptying out!"
@drink_amount = 0
end
def sip(sip_amount = 5)
puts "Taking a sip!"
if sip_amount > @drink_amount
puts "Oops, not much or any left! Sucking air. :("
@drink_amount = 0
else
@drink_amount -= sip_amount
end # if
end # sip method
# This is a helper method - if we didn't want to make @drink_amount readable directly, we can create a method that simply returns the value of @drink_amount
def amount
@drink_amount
end
# This is a class method - prepend the method name with 'self.'
def self.what_am_i
puts "I am a cup!"
end
end
# cup1 = Cup.new
# # cup2 = Cup.new
# cup1.fill
# puts "cup1 has #{cup1.amount}"
# # puts "cup2 has #{cup2.amount}"
# cup1.empty
# cup1.sip
# puts "cup1 has #{cup1.amount}"
# cup1.sip(10)
# puts "cup1 has #{cup1.amount}"
# # cup1.empty
# # puts "cup1 has #{cup1.amount}"