What’s the preferred method to add module functionality to library ruby code?
Consider the following:
module MyExceptions
class SomethingBadHappenedTheLibarayDesignerDidntConsider < StandardError; end
end
How can I add MyExceptions module to a class that I don’t control?
Update, in my_library_class_exceptions.rb I did:
class LibraryClass
include MyExceptions
end
module MyExceptions
class SomethingBadHappenedTheLibarayDesignerDidntConsider < StandardError; end
end
But the console returns: NameError: uninitialized constant LibraryClass::MyExceptions
I got this working by fixing a few things, and changing a few things.
It turns out
LibraryClasswasn’t actually a class, but a module. So I changed:class LibraryClass
include MyExceptions
end
into to:
library_class.rbintolib\library_class_extensions.rband made sure thatlibwas part of the autoload path.require 'library_class_extensions'I think that’s a better pattern for the behavior I was trying to accomplish anyway, since this really is more of library type code that
app/modeltype code.I’m up-voting the answers given by everyone, for their heroic efforts.