Ruby - Including two modules that both have a sub-module with the same name
You can always rename modules to not conflict:
Foo::FooErrors = Foo::Errors
Bar::BarErrors = Bar::Errors
Or you could skip the conflicting modules and go straight to including the error classes you want:
class Baz
include Foo::Errors
include Bar::Errors
end
Or don't bother including anything and use the full names:
p Foo::Errors::FooError.new
p Bar::Errors::BarError.new
In my opinion, include
is too often used as a convenience method to avoid typing module names. This usage is not really necessary and can introduce errors (as you've seen) or ambiguity. There are some things that you can only accomplish using include
(like adding instance methods from a module), but most uses I see don't fall in that camp.
How do I include a module in a namespaced class?
Use ::
to force the lookup to the top level only:
module Bar
class Foo
include ::Foo::Baz
end
end
Related Topics
Make Rails Ignore Daylight Saving Time When Displaying a Date
How to Store an Instance Variable Across Multiple Actions in a Controller
Difference Between or and || When Setting Variables
Errno::Eaccess: Permission Denied @ Dir_S_Mkdir
Rails 3.1 Load CSS in Particular Order
Enable Dropping a File Onto a Ruby Script
Encoding::Undefinedconversionerror: "\Xe4" from Ascii-8Bit to Utf-8
Failed to Build Gem Native Extension - Extconf.Rb Not Found
Multiple Applications Using a Single Code Base in Ruby
Getting Headers from a Ruby Net::Http Request Before Making the Request
Dynamic Active Record Store Accessors Based Off a User Form
Ruby on Linux Pty Goes Away Without Eof, Raises Errno::Eio
How to Model a Mutual Friendship in Rails
Ruby String Split with Terminal Strings Empty
Rails 3.1 - Changing Default Scaffold Views and Template
All Possible Combinations of Selected Character Substitution in a String in Ruby
Difference Between Lambda and -> Operator in Ruby
Rails 3, Displaying Jpg Images from the Local File System Above Rails_Root