Created
October 8, 2012 11:25
-
-
Save toamitkumar/3852022 to your computer and use it in GitHub Desktop.
open_range
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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