Skip to content

Instantly share code, notes, and snippets.

@toamitkumar
Created October 8, 2012 11:25
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 toamitkumar/3852022 to your computer and use it in GitHub Desktop.
Save toamitkumar/3852022 to your computer and use it in GitHub Desktop.
open_range
class OpenEndedRange
attr_reader :first, :last
def initialize(first, last, exists = {})
exists = { first => true, last => true }.merge(exists)
@first, @last, @first_exists, @last_exists = first, last, exists[:first], exists[:last]
end
def first_exists?
@first_exists
end
def last_exists?
@last_exists
end
def include?(other)
case [first_exists?, last_exists?]
when [true, true]
first <= other && other <= last
when [true, false]
first <= other
when [false, true]
other <= last
when [false, false]
true
end
end
alias_method :===, :include?
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment