Skip to content

Instantly share code, notes, and snippets.

@jstirk
Forked from mikel/example.rb
Created May 25, 2012 03:17
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 jstirk/2785549 to your computer and use it in GitHub Desktop.
Save jstirk/2785549 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 :trollface:
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