Created
May 16, 2012 17:04
-
-
Save arturaz/2712236 to your computer and use it in GitHub Desktop.
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
# Fake _target_ identity. This extends #is_a?, #kind_of?, #instance_of? on | |
# _target_ and #=== on _pretended_klass_. | |
# | |
# Example: fake_identity YourCustomHash, Hash | |
# | |
# @param [Class] target class that is being given fake identity | |
# @param [Class] pretended_klass class that target is pretending to be | |
def self.fake_identity(target, pretended_klass) | |
target.instance_eval do | |
[:is_a?, :kind_of?, :instance_of?].each do |method| | |
define_method(method) do |klass| | |
klass == pretended_klass ? true : super(klass) | |
end | |
end | |
end | |
metaclass = class << pretended_klass; self; end | |
metaclass.instance_eval do | |
define_method(:===) do |object| | |
if object.is_a?(target) | |
true | |
else | |
super(object) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment