Skip to content

Instantly share code, notes, and snippets.

@NathanZook
Last active August 29, 2015 13:57
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 NathanZook/9797293 to your computer and use it in GitHub Desktop.
Save NathanZook/9797293 to your computer and use it in GitHub Desktop.
Overcoming an identity crisis
class Role < BasicObject
# Nope, these role objects are not well-behaved objects __at_all__
alias_method :role_eql?, :eql?
def eql?(x)
x.eql? @data_object
end
def hash
@data_object.hash
end
alias_method :role_eq, :==
alias_method :role_ne, :!=
def ==(x)
x == @data_object
end
alias_method :role_equal?, :equal?
def equal?(x)
x.equal? @data_object
end
alias_method :role__id__, :__id__
def __id__
@data_object.__id__
end
alias_method :object_id, :__id__
# <=> is not meaningful for roles in general
def <=>(x)
inverse = @data_object <=> x
inverse ? -inverse : inverse
end
alias_method :role_case_eq, :===
def ===(x)
x === @data_object
end
# =~ is not meaningful for roles in general
def =~(x)
x =~ @data_object
end
klass = self
define_method(:role_class){ klass }
def class
@data_object.class
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment