Skip to content

Commit 5923417

Browse files
committed
add dog class
1 parent 674f661 commit 5923417

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

dog.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Dog
2+
3+
def initialize(color, type)
4+
@color, @type = color, type
5+
@bones = []
6+
end
7+
8+
def give(bone)
9+
# ensuring that the dog has less than three bones in order to accept a new
10+
# one
11+
if @bones.length < 3
12+
# add the bone to the bones array
13+
@bones << bone
14+
else
15+
puts "Too many bones!"
16+
end
17+
end
18+
19+
def eat
20+
if @bones.length == 0
21+
puts "There are no bones!"
22+
else
23+
# the pop method on the array will remove and return the last element
24+
# in the array. In this case @bones.pop will give us back a "bone" object
25+
# the "bone" object has a "size" attribute.
26+
puts "I ate a #{@bones.pop.size} bone!"
27+
end
28+
end
29+
30+
end

0 commit comments

Comments
 (0)