Created
May 7, 2012 18:57
-
-
Save gazay/2629683 to your computer and use it in GitHub Desktop.
Realisations of Range#=== in ruby18 and ruby19
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
# 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