W
William Morgan
Dear gurus,
I often add utility methods to Hash, Enumerable, etc. For applications
this is fine, but for a library, I feel like I should avoid leaving such
"method detritus" around in users' namespaces.
Is there a way to extend/modify existing classes or modules only within
the context of a particular ("library") module?
My first idea had some problems:
module Library
class Hash < ::Hash
def extramethod
# ...
end
end
Hash.new.extramethod # fine
{}.extramethod # problem, but I can work around
[1,2,3].map { |x| x + 1 }.extramethod # big problem
end
How do others deal with this?
Thanks for any insight,
I often add utility methods to Hash, Enumerable, etc. For applications
this is fine, but for a library, I feel like I should avoid leaving such
"method detritus" around in users' namespaces.
Is there a way to extend/modify existing classes or modules only within
the context of a particular ("library") module?
My first idea had some problems:
module Library
class Hash < ::Hash
def extramethod
# ...
end
end
Hash.new.extramethod # fine
{}.extramethod # problem, but I can work around
[1,2,3].map { |x| x + 1 }.extramethod # big problem
end
How do others deal with this?
Thanks for any insight,