-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathfoo_bar.rb
59 lines (49 loc) · 1.51 KB
/
foo_bar.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
# coding: utf-8
FOO = 123
module Foo
FOO = 555
end
module Foo
class Bar
def baz
puts FOO
end
end
end
class Foo::Bar
def glorf
puts FOO
end
end
Foo::Bar.new.baz
# => 555
Foo::Bar.new.glorf
# => 123
# You can think of each appearance of `module Something`, `class Something` or
# `def something` as a “gateway” into a new scope. When Ruby is searching for the
# definition of a name that has been referenced it first looks in the current
# scope (the method, class or module), and if it isn’t found there it will go
# back through each containing “gateway” and search the scope there.
# In your example the method `baz` is defined as
module Foo
class Bar
def baz
puts FOO
end
end
end
# So when trying to determine the value of `FOO`, first the class `Bar` is
# checked, and since `Bar` doesn’t contain a `FOO` the search moves up through
# the class Bar "gateway” into the `Foo` module which is the containing scope.
# `Foo` does contain a constant `FOO` (555) so this is the result you see.
# The method `glorf` is defined as:
class Foo::Bar
def glorf
puts FOO
end
end
# Here the “gateway” is class Foo::Bar, so when `FOO` isn’t found inside `Bar`
# the “gateway” passes through the `Foo` module and straight into the top level,
# where there is another `FOO` (123) which is what is displayed. Note how using
# `class Foo::Bar` creates a single “gateway”, skipping over the scope of `Foo`,
# but `module Foo; class Bar` ... opens two separate “gateways”