Skip to content

Instantly share code, notes, and snippets.

@mikel
Created May 25, 2012 03:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mikel/2785503 to your computer and use it in GitHub Desktop.
Save mikel/2785503 to your computer and use it in GitHub Desktop.
Range Fun in Ruby
(-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 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