Skip to content

Instantly share code, notes, and snippets.

@jordansissel
Created June 26, 2013 22:54
Show Gist options
  • Save jordansissel/5872482 to your computer and use it in GitHub Desktop.
Save jordansissel/5872482 to your computer and use it in GitHub Desktop.
module Foo
class Error < StandardError; end
end
module Foo
class Baz
def initialize
puts Error
end
end
end
class Foo::Bar
def initialize
puts Error
end
end
# Works:
puts Foo::Baz.new
# Fails: uninitialized constant Foo::Bar::Error
puts Foo::Bar.new
@pcarrier
Copy link

Duh. Duh? Duh!

"class Foo::Bar; end" is in the scope of your file. Why? Because you never opened another scope.

"class Baz; end" is in the scope of the Foo module. Why? Because you opened it with "module Foo".

Where is the WTF?

@jbbarth
Copy link

jbbarth commented Jun 28, 2013

Sorry but that's not obvious, a lot of people think the second construct is a shortcut for the first. Including me-five-minutes-ago. Principle of least surprise anyone ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment