Skip to content

Instantly share code, notes, and snippets.

Created December 22, 2012 01:26
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 anonymous/614ccbbe263cd1196203 to your computer and use it in GitHub Desktop.
Save anonymous/614ccbbe263cd1196203 to your computer and use it in GitHub Desktop.
require 'active_support/concern'
require 'active_support/core_ext/object/acts_like'
module InfiniteComparable
extend ActiveSupport::Concern
included do
class_exec do
alias_method_chain :<=>, :other
end
end
define_method :'<=>_with_other' do |other|
return self.send(:'<=>_without_other', other) if other.class == self.class
conversion = :"to_#{self.class.name.downcase}"
if other.respond_to?(:infinite?) && other.infinite?
-other.infinite?
elsif other.respond_to?(conversion)
self.send(:'<=>_without_other', other.send(conversion))
else
self.send(:'<=>_without_other', other)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment