Last active
December 15, 2015 04:59
-
-
Save darthschmoo/5205696 to your computer and use it in GitHub Desktop.
Ruby: Trying to add some decorations to core classes, but only have them apply within a specific module.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The result I'm looking for | |
module Herp | |
"".respond_to?( :whizbangify ) # true | |
File.respond_to?( :take_ownership ) # true | |
end | |
"".respond_to?( :whizbangify ) # false | |
File.respond_to?( :take_ownership ) # false | |
# What I tried | |
module CustomString | |
def whizbangify | |
puts "whizbangify" | |
end | |
end | |
module CustomFile | |
def self.take_ownership | |
puts "take ownership" | |
end | |
end | |
module Herp | |
class HerpString < String | |
include CustomString | |
end | |
class HerpFile < File | |
include CustomFile | |
end | |
end | |
Herp::String = Herp::HerpString | |
Herp::File = Herp::HerpFile | |
"".respond_to?( :whizbangify ) # false (correct) | |
Herp::HerpString.new.respond_to?(:whizbangify) # true (correct) | |
Herp::String.new.respond_to?(:whizbangify) # true (correct) | |
module Herp | |
def self.test | |
"".respond_to?( :whizbangify ) | |
end | |
end | |
Herp.test # false (not the behavior I hoped for) | |
# So, is there a way to get this behavior? I was hoping that, within the module, | |
# any references to String would go to HerpString first, but it doesn't seem to | |
# work that way. I've been getting "superclass mismatch" errors. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment