[#116016] [Ruby master Bug#20150] Memory leak in grapheme clusters — "peterzhu2118 (Peter Zhu) via ruby-core" <ruby-core@...>
Issue #20150 has been reported by peterzhu2118 (Peter Zhu).
7 messages
2024/01/04
[#116382] [Ruby master Feature#20205] Enable `frozen_string_literal` by default — "byroot (Jean Boussier) via ruby-core" <ruby-core@...>
Issue #20205 has been reported by byroot (Jean Boussier).
77 messages
2024/01/23
[ruby-core:116464] [Ruby master Feature#20080] Introduce #bounds method on Range
From:
"AMomchilov (Alexander Momchilov) via ruby-core" <ruby-core@...>
Date:
2024-01-27 17:34:17 UTC
List:
ruby-core #116464
Issue #20080 has been updated by AMomchilov (Alexander Momchilov).
Could we implement this as `#deconstruct`, so Ranges can support destructuring?
```ruby
class Range
def deconstruct = [self.begin, self.end]
end
case 1..2
in [1, Integer => upper]
p "matched: #{upper}"
else
p "not matched"
end
```
----------------------------------------
Feature #20080: Introduce #bounds method on Range
https://bugs.ruby-lang.org/issues/20080#change-106484
* Author: stuyam (Stuart Yamartino)
* Status: Open
* Priority: Normal
----------------------------------------
Followup Reference: #20027
**Updated Proposal:** (based on many wonderful suggestions!)
1. Call the method `#bounds`.
```ruby
first, last = (1..300).bounds # => [1, 300]
first, last = (300..1).bounds # => [300, 1]
first, last = (..300).bounds # => [nil, 300]
first, last = (1..).bounds # => [1, nil]
```
2. Add `exclude_end?` support so re-hydration of Range works:
```ruby
b = (1..2).bounds #=> [1,2]
Range.new(*b) #=> 1..2
b = (1...2).bounds #=> [1,2,true]
Range.new(*b) #=> 1...2
```
I did a better job of outlining use cases in this comment below so I will let that speak for itself: https://bugs.ruby-lang.org/issues/20080#note-3
**Original Proposal:**
This feature request is to implement a method called `#begin_and_end` on `Range` that returns an array of the first and last value stored in a range:
```ruby
(1..300).begin_and_end #=> [1, 300]
first, last = (300..1).begin_and_end
first #=> 300
last #=> 1
```
I believe this would be a great addition to Ranges as they are often used to pass around a single object used to hold endpoints, and this allows easier retrieval of those endpoints.
This would allow easier deconstruction into start and end values using array deconstruction as well as a simpler way to serialize to a more primitive object such as an array for database storage.
This implementation was suggested by @mame in my initial feature suggestion regarding range deconstruction: https://bugs.ruby-lang.org/issues/20027
This implementation would work similar to how `#minmax` works where it returns an array of two numbers, however the difference is that `#minmax` doesn't work with reverse ranges as @Dan0042 pointed out in the link above:
```ruby
(1..42).minmax #=> [1, 42]
(42..1).minmax #=> [nil, nil]
```
--
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/