forked from jsfowles/ruby_classes_modules_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclasses_2.rb
More file actions
99 lines (67 loc) · 1.39 KB
/
Copy pathclasses_2.rb
File metadata and controls
99 lines (67 loc) · 1.39 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# base class
class Mammal
attr_accessor :gender, :name, :age, :alive
# optional or multiple optional peramiters have to be at the end after the required ones
def initialize(gender, name, age, alive = true)
@gender = gender
@name = name
@age = age
@alive = alive
end
#These are instance methods
def speak
raise 'You must implement this in a subclass'
end
def increment_age(age = 1)
@age += age
end
def kill
@alive = false if @alive
end
#This is a class method
def self.type_of_blood
puts 'Mammals are warm blooded'
end
end
# These are sub classes
# human
class Human < Mammal
def initialize(gender, name, age, alive, hair_color)
super(gender, name, age, alive)
@hair_color = hair_color
end
def speak
puts "Esto no es ingles."
end
end
# elephant
class Elephant < Mammal
def initialize(gender, name, age, alive)
super(gender, name, age, alive)
end
def speak
puts "Trumpeting..."
end
end
# cat
class Cat < Mammal
def initialize(gender, name, age, alive, fur_color)
super(gender, name, age, alive)
@fur_color = fur_color
end
def speak
puts "Meow..."
end
end
linds = Human.new('female', 'Lindsey', 26, true, 'Brown')
linds.speak
linds.increment_age
puts linds.alive
linds.kill
puts linds.alive
Mammal.type_of_blood
jax = Cat.new('Male', 'Jax', 2, true, 'B&W')
jax.speak
dumbo = Elephant.new('Male', 'Dumbo', 1, true)
dumbo.speak
puts jax.kill