Created
May 25, 2012 03:04
-
-
Save mikel/2785503 to your computer and use it in GitHub Desktop.
Range Fun in Ruby
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
(-10..-5).include?(-7) | |
#=> true | |
(-5..-10).include?(-7) | |
#=> false | |
(10..5).include?(7) | |
#=> false | |
(5..10).include?(7) | |
#=> true | |
# OK, so maybe 10..5 wraps the whole set of integers? | |
(10..5).include?(3) | |
#=> false | |
(10..5).include?(11) | |
#=> false | |
# Nope... |
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
// This shows why this is so | |
range_cover(VALUE range, VALUE val) | |
{ | |
VALUE beg, end; | |
beg = RANGE_BEG(range); | |
end = RANGE_END(range); | |
if (r_le(beg, val)) { | |
if (EXCL(range)) { | |
if (r_lt(val, end)) | |
return Qtrue; | |
} | |
else { | |
if (r_le(val, end)) | |
return Qtrue; | |
} | |
} | |
return Qfalse; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment