Skip to content

Instantly share code, notes, and snippets.

@arturaz
Created May 16, 2012 17:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arturaz/2712236 to your computer and use it in GitHub Desktop.
Save arturaz/2712236 to your computer and use it in GitHub Desktop.
# 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