forked from ga-wdi-exercises/checkpoint-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoop.rb
More file actions
55 lines (41 loc) · 1.15 KB
/
oop.rb
File metadata and controls
55 lines (41 loc) · 1.15 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
# Question 6
# Define a Ruby class called `Animal`. Each `Animal` should have...
# - A `name` (String) attribute
# - A `greet` instance method
# - The ability to "get" and "set" `name`
# Type your solution directly below this line:
class Animal
attr_accessor :name
def initialize (name)
@name = name
end
def greet
puts "Hello, my name is #{@name}."
end
end
# Question 7
# Create a new `Animal` instance with the name "Pumba".
# Type your solution directly below this line:
pumba = Animal.new("Pumba")
# Question 8
# Define a Ruby class called `Lion` that inherits from the `Animal` class.
# Each lion should have the same attributes and methods as `Animal`. Each lion
# should also have...
# - A `king` (Boolean) attribute
# - Only set the `king` attribute to `true` if the instance's `name` is "Simba"
# Type your solution directly below this line:
class Lion < Animal
def initialize (name)
super (name)
@king =
if name == "Simba"
true
else
false
end
end
end
# Question 9
# Create a new instance of `Lion` with the name "Simba".
# Type your solution directly below this line:
simba = Lion.new("Simba")