开发者

Inconsistency between ruby 1.8.7 and 1.9.2 Date range when comparing a DateTime

开发者 https://www.devze.com 2023-02-15 14:35 出处:网络
Ruby 1.8.7: 开发者_StackOverflow中文版> r = (Date.civil(2010, 12, 1)..Date.civil(2010, 12, 31))

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

0

精彩评论

暂无评论...
验证码 换一张
取 消