G
Gavin Kistner
--Apple-Mail-1-556835652
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
charset=US-ASCII;
delsp=yes;
format=flowed
I have a method that requires that the arguments passed to it have
been in-mixed with a module. Currently, my code looks like this:
def my_method( the_obj )
the_obj.extend( MyModule ) unless the_obj.inherits_from?
( MyModule )
end
class Object
def selfclass
class << self; self; end
end
# Return true if the instance inherits from the supplied
_class_or_module_.
def inherits_from?( class_or_module )
self.class.ancestors.include?( class_or_module ) or
self.selfclass.ancestors.include?( class_or_module )
end
end
Is there a better way to do that? (I'm testing if the object inherits
from a class which was extended by the module, OR if the instance
itself was already extended.) Does it end up being cheaper/faster to
just always re-extend the instance? (What happens if you extend both
the class and an instance of that class? Does the singleton class
take precedence?)
--
"When I am working on a problem I never think about beauty. I only
think about how to solve the problem. But when I have finished, if
the solution is not beautiful, I know it is wrong."
- R. Buckminster Fuller
--Apple-Mail-1-556835652--
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
charset=US-ASCII;
delsp=yes;
format=flowed
I have a method that requires that the arguments passed to it have
been in-mixed with a module. Currently, my code looks like this:
def my_method( the_obj )
the_obj.extend( MyModule ) unless the_obj.inherits_from?
( MyModule )
end
class Object
def selfclass
class << self; self; end
end
# Return true if the instance inherits from the supplied
_class_or_module_.
def inherits_from?( class_or_module )
self.class.ancestors.include?( class_or_module ) or
self.selfclass.ancestors.include?( class_or_module )
end
end
Is there a better way to do that? (I'm testing if the object inherits
from a class which was extended by the module, OR if the instance
itself was already extended.) Does it end up being cheaper/faster to
just always re-extend the instance? (What happens if you extend both
the class and an instance of that class? Does the singleton class
take precedence?)
--
"When I am working on a problem I never think about beauty. I only
think about how to solve the problem. But when I have finished, if
the solution is not beautiful, I know it is wrong."
- R. Buckminster Fuller
--Apple-Mail-1-556835652--