From: "stuyam (Stuart Yamartino) via ruby-core" Date: 2023-12-22T18:57:31+00:00 Subject: [ruby-core:115871] [Ruby master Feature#20080] Implement #begin_and_end method on Range Issue #20080 has been updated by stuyam (Stuart Yamartino). @Dan0042 great idea! At first I was against this because I thought it would make deconstruction harder but it actually wouldn't because deconstruction would work the same. I was thinking the second value if deconstructed would be an array like `[2, true]` but that would only be if you used a star `*` during deconstruction. I'm liking this... ```ruby a = (1..2).bounds #=> [1,2] b = (1...2).bounds #=> [1,2,true] Range.new(*a) #=> 1..2 Range.new(*b) #=> 1...2 start_num, end_num = a #=> start_num = 1, end_num = 2 start_num, end_num, excluded_end = a #=> start_num = 1, end_num = 2, excluded_end = nil start_num, end_num = b #=> start_num = 1, end_num = 2 start_num, end_num, excluded_end = b #=> start_num = 1, end_num = 2, excluded_end = true start_num, *remaining = b #=> start_num = 1, remaining = [2, true] ``` My only thought then is should there be an option to always include `exclude_end?` even when it is false like `(1..2).bounds(true) #=> [1,2,false]` so you would get `false` rather than `nil` in the array deconstruction example above. ---------------------------------------- Feature #20080: Implement #begin_and_end method on Range https://bugs.ruby-lang.org/issues/20080#change-105828 * Author: stuyam (Stuart Yamartino) * Status: Open * Priority: Normal ---------------------------------------- Followup Reference: #20027 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/