-
Notifications
You must be signed in to change notification settings - Fork 0
/
default_object_self.rb
59 lines (47 loc) · 1.18 KB
/
default_object_self.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
57
58
59
# Source: Book - Well-Grounded Rubyist
# Takeaways
# 1. `self` is the current or default object
# 2. At any point, there is one and only one `self` object
# 3.
# Top level of program
puts '# Top level of program'
puts "'self' is #{self}"
# Module definition
puts "\n# Module definition"
module ModuleSelf
puts "'self' is #{self}, the module object"
end
class ModuleSelfTest
include ModuleSelf
end
mst = ModuleSelfTest.new
# Class definition
puts "\n# Class definition"
class ClassSelf
puts "'self' is #{self}, the class object"
end
cs = ClassSelf.new
# Method definition - Class Singleton
puts "\n# Method definition - Class Singleton"
class MethodSingleton
def self.methodSelf
puts "'self' is #{self}, the class object"
end
end
MethodSingleton.methodSelf
# Method definition - Object Singleton
puts "\n# Method definition - Object Singleton"
obj = Object.new
def obj.methodSelf
puts "'self' is #{self}, this specific object"
end
obj.methodSelf
# Method definition - Instance
puts "\n# Method definition - Instance"
class MethodInstance
def methodSelf
puts "'self' is #{self}, an instance of this class"
end
end
ms = MethodInstance.new
ms.methodSelf