From: "andrykonchin (Andrew Konchin)" Date: 2022-11-11T17:29:41+00:00 Subject: [ruby-core:110710] [Ruby master Bug#19123] Error handling of Struct#values_at and Array#values_at is slightly inconsistent Issue #19123 has been reported by andrykonchin (Andrew Konchin). ---------------------------------------- Bug #19123: Error handling of Struct#values_at and Array#values_at is slightly inconsistent https://bugs.ruby-lang.org/issues/19123 * Author: andrykonchin (Andrew Konchin) * Status: Open * Priority: Normal * Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN ---------------------------------------- `Struct#values_at` and `Array#values_at look pretty similar and handle all the complex cases of arguments (integer Ranges, list of Integers and mixing Ranges and Integers) in the same way. Error handling is similar as well. In case of invalid Range argument they behave identically: ```ruby clazz = Struct.new(:name, :director, :year) movie = clazz.new('Sympathy for Mr. Vengeance', 'Chan-wook Park', 2002) array = [0, 1, 2] # end is out of range movie.values_at(0..4) # => ["Sympathy for Mr. Vengeance", "Chan-wook Park", 2002, nil, nil] array.values_at(0..4) # => [0, 1, 2, nil, nil] # beginning is out of range movie.values_at(-5..4) # -5..4 out of range (RangeError) array.values_at(-5..4) # -5..4 out of range (RangeError) ``` But when Integer argument is passed - they handle out of range cases differently: ```ruby # end is out of range movie.values_at(4) # => offset 4 too large for struct(size:3) (IndexError) array.values_at(4) # => [nil] # beginning is out of range movie.values_at(-5) # => offset -5 too small for struct(size:3) (IndexError) array.values_at(-5) # => [nil] ``` So I am wondering what is the reason of such inconvenience. I suppose there may be a reason of this difference. But I see some benefits in consistent error handling. -- https://bugs.ruby-lang.org/ Unsubscribe: