Forum: Ruby-core Meaningless nil range

D51b68da10020e8eb90b6315a0664e81?d=identicon&s=25 Pascal Hurni (Guest)
on 2013-03-28 11:44
(Received via mailing list)

I came accross a bizarre behaviour when porting my code from 1.8.7 to

Here are irb sessions:

=> "1.8.7"
 >> nil..nil
ArgumentError: bad value for range
     from (irb):2

2.0.0p0 :002 > range = nil..nil
  => nil..nil
2.0.0p0 :003 > range.include? nil
TypeError: can't iterate from NilClass
     from (irb):3:in `each'
     from (irb):3:in `include?'
     from (irb):3:in `include?'
     from (irb):3
     from /Users/phi/.rvm/rubies/ruby-2.0.0-p0/bin/irb:16:in `<main>'

So a range of nil..nil throws an error in 1.8.7 but does not in 2.0.0.
While it is accepted in 2.0.0, nothing meaningful may be done with it.
I'd highly prefer the 1.8.7 behaviour of throwing an error when
constructing such a range.

What are your opinions?


86c07aa43c6df798df005edd84ee8b56?d=identicon&s=25 Marc-Andre Lafortune (Guest)
on 2013-03-28 15:25
(Received via mailing list)
In 1.9+, you can indeed create ranges that are not very meaningful with
objects. The method `cover?` works, though.

    o =
    r = o..o
    r.cover?(o) # => true

I don't see a good way to forbid `nil..nil` and remain generic, though.

You should open a feature request if you feel strongly about this.
This topic is locked and can not be replied to.