-
Notifications
You must be signed in to change notification settings - Fork 0
/
classes.rb
56 lines (46 loc) · 1.36 KB
/
classes.rb
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
# p029dog.rb
# define class Dog
class Dog
def initialize(breed, name)
# Instance variables
@breed = breed
@name = name
end
def bark
puts 'Ruff! Ruff!'
end
def display
puts "I am of #{@breed} breed and my name is #{@name}"
end
end
# make an object
# Objects are created on the heap
d = Dog.new('Labrador', 'Benzy')
=begin
Every object is "born" with certain innate abilities.
To see a list of innate methods, you can call the methods
method (and throw in a sort operation, to make it
easier to browse visually). Remove the comment and execute.
=end
puts d.methods.sort
# Amongst these many methods, the methods object_id and respond_to? are important.
# Every object in Ruby has a unique id number associated with it
puts "The id of d is #{d.object_id}."
# To know whether the object knows how to handle the message you want
# to send it, by using the respond_to? method.
if d.respond_to?("talk")
d.talk
else
puts "Sorry, d doesn't understand the 'talk' message."
end
d.bark
d.display
# making d and d1 point to the same object
d1 = d
d1.display
# making d a nil reference, meaning it does not refer to anything
d = nil
d.display
# If I now say
d1 = nil
# then the Dog object is abandoned and eligible for Garbage Collection (GC)