在比较DateTime时,ruby 1.8.7和1.9.2日期范围之间不一致

Mac*_*rio 1 ruby

Ruby 1.8.7:

> r = (Date.civil(2010, 12, 1)..Date.civil(2010, 12, 31))
> r.include? DateTime.civil(2010,12,28,15,0)
=> true
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

任何人都知道为什么会这样?,我非常喜欢1.8.7行为,这种不一致打破了我的一些代码:(

Dyl*_*kow 6

RangeRuby 1.9中的对象行为不同.以前,Range#include?实际上只是进行了大于/小于比较.但是,现在,它会迭代范围内的每个项目(在本例中为日期),并将您的值与每个项目进行比较.

Ruby 1.9已添加Range#cover?,其行为类似于1.8版本Range#include?- 但是,它不向后兼容Ruby 1.8.

> r.cover? DateTime.civil(2010,12,28,15,0)
=> true
Run Code Online (Sandbox Code Playgroud)

更多信息:http://rhnh.net/2009/08/03/range-include-in-ruby-1-9