Project

General

Profile

Actions

Misc #18984

open

Doc for Range#size for Float/Rational does not make sense

Added by masasakano (Masa Sakano) 3 months ago. Updated about 2 months ago.

Status:
Open
Priority:
Normal
Assignee:
-
[ruby-core:109773]

Description

When Range consists of any Numeric, according to Official docs for Ruby-3.1.2, Range#size should,

Returns the count of elements in self if both begin and end values are numeric;

Indeed, when Range consists of only Integer, this makes sense.
However, when the begin value of Range is a Float or Rational, "the count of elements" does not make sense. Such Ranges are not iteratable, suggesting there are no such things as "elements":

(0.51..5.quo(2)).each{}  # => TypeError

Yet, Range#size of such Ranges returns an Integer

(0.51..5.quo(2)).size  # => 2

It seems both begin and end values of a Range are rounded (Numeric#round) to the nearest Integer before Range#size is calculated.

If this is the specification, I suggest it should be clearly stated in the Official docs, avoiding the confusing expression "the count of elements", because "elements" are not unambiguously defined for Range of Float/Rational.

Updated by Eregon (Benoit Daloze) 3 months ago

  • Assignee deleted (ruby-core)

Updated by Eregon (Benoit Daloze) 3 months ago

Please do not assign ruby-core, that sends too many emails.

Updated by sawa (Tsuyoshi Sawada) about 2 months ago

I think this is a bug. Since (0.51..5.quo(2)).count raises a TypeError and (0.51..5.quo(2)).size is supposed to follow that, it should raise a TypeError as well.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0