Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@gazay
Created May 7, 2012 18:57
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 gazay/2629683 to your computer and use it in GitHub Desktop.
Save gazay/2629683 to your computer and use it in GitHub Desktop.
Realisations of Range#=== in ruby18 and ruby19
# implementation of Range#=== in 1.8.7 - it was the same method as Range#include?:
<<-EOS
static VALUE
range_include(range, val)
VALUE range, val;
{
VALUE beg, end;
beg = rb_ivar_get(range, id_beg);
end = rb_ivar_get(range, id_end);
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;
}
EOS
# so when it was called after aliasing original include - it was ok
# rails3, ruby 1.8.7:
(1..5).include? (2..4)
# => true
(1..5) === (2..4)
# => false
# But in ruby19 it was rewritten and Range#=== is just caller for original include method from C source:
<<-EOS
static VALUE
range_eqq(VALUE range, VALUE val)
{
return rb_funcall(range, rb_intern("include?"), 1, val);
}
EOS
# And now if we do aliasing on include?, Range#=== will call new include? method instead of old realisation
# rails3, ruby19:
(1..5).include? (2..4)
# => true
(1..5) === (2..4)
# => true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment