Skip to content

Instantly share code, notes, and snippets.

@mathie
Created October 14, 2010 07:57
Show Gist options
  • Save mathie/625830 to your computer and use it in GitHub Desktop.
Save mathie/625830 to your computer and use it in GitHub Desktop.
#!/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