Project

General

Profile

Bug #12090

`Range#cover` does not raise an exception when comparison fails

Added by sawa (Tsuyoshi Sawada) over 3 years ago. Updated 24 days ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:73894]

Description

The documentation for Range#cover? says,

cover?(obj) → true or false

Returns true if obj is between the begin and end of the range.

This tests begin <= obj <= end when exclude_end? is false and begin <= obj < end when exclude_end? is true.

which implies that, when comparison fails, cover? should raise an error. But actually, it doesn't:

"a" <= 1 # => ArgumentError: comparison of String with 1 failed
1 <= "a" # => ArgumentError: comparison of Fixnum with String failed
("a".."z").cover?(1) # => false

It silently returns false instead of raising an error.

Either Range#cover? should raise an error when comparison fails, or the documentation should be changed to correctly describe this behaviour.

Associated revisions

Revision 3556cba5
Added by jeremyevans (Jeremy Evans) 24 days ago

Document that Range#cover? returns false if <=> returns nil

Fixes [Bug #12090]

History

Updated by sos4nt (Stefan Schüßler) over 3 years ago

Raising an error would break backward compatibility. Maybe cover? should just return nil to indicate incomparable values.

("a".."z").cover?("c")  # => true
("a".."z").cover?("5")  # => false
("a".."z").cover?(1)    # => nil
#2

Updated by jeremyevans (Jeremy Evans) 24 days ago

  • Status changed from Open to Closed

Applied in changeset git|3556cba5038530e728eb6309dcf5d4d1a96a02ac.


Document that Range#cover? returns false if <=> returns nil

Fixes [Bug #12090]

Also available in: Atom PDF