Skip to content

Instantly share code, notes, and snippets.

@notEthan
Created December 14, 2024 00:11
Show Gist options
  • Save notEthan/f3a2909aa414158762911cedade30c08 to your computer and use it in GitHub Desktop.
Save notEthan/f3a2909aa414158762911cedade30c08 to your computer and use it in GitHub Desktop.
ruby module constant inclusion
module Scorpio
module Errors
class HTTPError < StandardError
end
end
end
module Scorpio
include Errors # ⬅️ defines Scorpio::HTTPError (or does it?)
end
Scorpio::HTTPError
#=> Scorpio::Errors::HTTPError
Scorpio.const_defined?(:HTTPError)
#=> true
module Scorpio
HTTPError
end
#=> Scorpio::Errors::HTTPError
module Scorpio
module Resource
HTTPError # ⬅️ NameError - I'd expect this to resolve as Scorpio::HTTPError,
# but Resource can't see HTTPError on Scorpio's namespace for some reason.
# I'd expect a NameError from `module Scorpio::Resource; HTTPError; end`
# but seems like this way ought to work
end
end
# in `<module:Resource>': uninitialized constant Scorpio::Resource::HTTPError (NameError)
# in `<module:Scorpio>'
module Scorpio
module Resource
Scorpio::HTTPError # works
end
end
#=> Scorpio::Errors::HTTPError
module Scorpio
module Resource
Errors # ⬅️ it sees Errors on Scorpio's namespace
Errors::HTTPError
end
end
#=> Scorpio::Errors::HTTPError
module Scorpio
HTTPError = HTTPError # ⬅️ insane thing to write
end
#=> Scorpio::Errors::HTTPError
module Scorpio
module Resource
HTTPError # ⬅️ now it resolves with Scorpio::HTTPError explicitly set
end
end
#=> Scorpio::Errors::HTTPError
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment