Skip to content

Instantly share code, notes, and snippets.

@apeiros
Created January 15, 2011 23:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apeiros/781361 to your computer and use it in GitHub Desktop.
Save apeiros/781361 to your computer and use it in GitHub Desktop.
A generic smaller/bigger implementation, for things like infinite ranges etc.
(1..Bigger).first(5) # => [1, 2, 3, 4, 5]
Bigger = Object.new
Bigger.extend Comparable
def Bigger.inspect; "#<Bigger>"; end
def Bigger.<=>(other); equal?(other) ? 0 : 1; end
def Bigger.coerce(other); [Smaller, self]; end
Smaller = Object.new
Smaller.extend Comparable
def Smaller.inspect; "#<Smaller>"; end
def Smaller.<=>(other); equal?(other) ? 0 : -1; end
def Smaller.coerce(other); [Bigger, self]; end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment