Feature #19698
open
Need an idiomatic way to define an empty range
Added by shouichi (Shouichi Kamiya) over 1 year ago.
Updated over 1 year ago.
Description
It seems there is no idiomatic way to define an empty range. We can define empty ranges in several ways such as
irb(main):001:0> (0...0).none?
=> true
irb(main):002:0> (1...-1).none?
=> true
I think the above examples do not communicate the intention well.
What is your “intention” to use an “empty” range?
And what do you think “idiomatic” way is?
What is your “intention” to use an “empty” range?
In a system I'm working on, I need to represent an empty date range. We can choose two arbitrary dates d1 and d2 where d1 > d2, then use d2 as a beginning and d1 as an end of a range. For example,
EMPTY_RANGE = Date.new(2000, 1, 1)..Date.new(1900, 1, 1)
But I don't think this communicates well because d1 and d2 are too arbitrary and those concrete dates only create noises when reading code.
And what do you think “idiomatic” way is?
One idea is to add a constant e.g., Range::EMPTY
just like Float::EPSILON
.
Also available in: Atom
PDF
Like1
Like1Like1