Created
March 25, 2015 02:50
-
-
Save bodhi/3fde34d207d7d36532fe to your computer and use it in GitHub Desktop.
I can never remember the scoping rules for Ruby, so here's an example to remind me.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module A | |
module X | |
def x | |
"X::x" | |
end | |
end | |
end | |
# Won't search in A for X, so need to scope it. | |
class A::B | |
include X rescue nil # :( | |
include A::X | |
def b | |
x | |
end | |
end | |
puts A::B.new.x | |
module U | |
module Y | |
def y | |
"Y::y" | |
end | |
end | |
end | |
# Will search for Y in U | |
module U | |
class V | |
include Y | |
def v | |
y | |
end | |
end | |
end | |
puts U::V.new.y |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment