Skip to content

Instantly share code, notes, and snippets.

@carlosantoniodasilva
Last active December 10, 2015 14:38
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 carlosantoniodasilva/4449379 to your computer and use it in GitHub Desktop.
Save carlosantoniodasilva/4449379 to your computer and use it in GitHub Desktop.
Infinite Comparable attempt to refactor.
define_method '<=>_with_infinity' do |other|
if other.class == self.class
public_send('<=>_without_infinity', other)
else
infinite = try(:infinite?)
other_infinite = other.try(:infinite?)
# inf <=> inf
if infinite && other_infinite
infinite <=> other_infinite
# not_inf <=> inf
elsif other_infinite
-other_infinite
# inf <=> not_inf
elsif infinite
infinite
else
conversion = "to_#{self.class.name.downcase}"
other = other.public_send(conversion) if other.respond_to?(conversion)
public_send('<=>_without_infinity', other)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment