A
Alexander
Range relies entirely on Enumerable to implement #min and #max, which
in turn implements these operations using #to_a. This becomes a
problem with ranges of floats, for example:
TypeError: can't iterate from Float
from (irb):9:in `each'
from (irb):9
from :0
Not to mention that it becomes inefficient to expand the whole range
into array, such as for very large integers:
(Tends to run out of memory.)
I think it makes more sense to implement these operations in terms of
<=3D>, like so:
class Range
def min
(self.first <=3D> self.last) <=3D 0 ? self.first : nil
end
def max
(self.first <=3D> self.last) <=3D 0 ? self.last : nil
end
end
I don't see any reason to rely on expanding the range using
Enumerable#to_a, since the operations can be implemented purely in
terms of the range boundaries.
I would be happy to submit a patch.
Alexander.
in turn implements these operations using #to_a. This becomes a
problem with ranges of floats, for example:
TypeError: can't iterate from Float
from (irb):9:in `each'
from (irb):9
from :0
Not to mention that it becomes inefficient to expand the whole range
into array, such as for very large integers:
(Tends to run out of memory.)
I think it makes more sense to implement these operations in terms of
<=3D>, like so:
class Range
def min
(self.first <=3D> self.last) <=3D 0 ? self.first : nil
end
def max
(self.first <=3D> self.last) <=3D 0 ? self.last : nil
end
end
I don't see any reason to rely on expanding the range using
Enumerable#to_a, since the operations can be implemented purely in
terms of the range boundaries.
I would be happy to submit a patch.
Alexander.