Ruby 1.8.7:
开发者_StackOverflow中文版> r = (Date.civil(2010, 12, 1)..Date.civil(2010, 12, 31))
> r.include? DateTime.civil(2010,12,28,15,0)
=> true
Ruby 1.9.2
> r = (Date.civil(2010, 12, 1)..Date.civil(2010, 12, 31))
> r.include? DateTime.civil(2010,12,28,15,0)
=> false
Anyone knows why is that?, I so much prefer 1.8.7 behaviour, this inconsistency broke some of my code :(
Range
objects in Ruby 1.9 act differently. Previously, Range#include?
really just did a greater-than/less-than comparison. Now, however, it iterates over each item in the range (in this case, dates), and compares your value to each one.
Ruby 1.9 has added Range#cover?
, which acts like the 1.8 version of Range#include?
-- however, it is not backwards compatible with Ruby 1.8.
> r.cover? DateTime.civil(2010,12,28,15,0)
=> true
More info: http://rhnh.net/2009/08/03/range-include-in-ruby-1-9
精彩评论