Created
October 14, 2010 07:57
-
-
Save mathie/625830 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env ruby | |
require 'test/unit' | |
CONSTANT = :global | |
module A | |
CONSTANT = :lexical | |
class B | |
def constant | |
CONSTANT | |
end | |
end | |
end | |
module A | |
class C | |
def constant | |
CONSTANT | |
end | |
end | |
end | |
# I know it happens, from experience, but I've never understood why declaring | |
# a class inside a module with the scope operator doesn't open up the lexical | |
# scope of the enclosing module (see the example below). So, why? | |
class A::D | |
def constant | |
CONSTANT | |
end | |
end | |
class LexicalTest < Test::Unit::TestCase | |
def test_b | |
assert_equal :lexical, A::B.new.constant | |
end | |
def test_c | |
assert_equal :lexical, A::C.new.constant | |
end | |
def test_d | |
assert_equal :global, A::D.new.constant | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment