ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:120257] [Ruby master Bug#20957] RangeError on Array#values_at with negative ranges
@ 2024-12-16  2:05 kyanagi (Kouhei Yanagita) via ruby-core
  2025-01-13 11:43 ` [ruby-core:120631] " johnnyshields (Johnny Shields) via ruby-core
  0 siblings, 1 reply; 2+ messages in thread
From: kyanagi (Kouhei Yanagita) via ruby-core @ 2024-12-16  2:05 UTC (permalink / raw)
  To: ruby-core; +Cc: kyanagi (Kouhei Yanagita)

Issue #20957 has been reported by kyanagi (Kouhei Yanagita).

----------------------------------------
Bug #20957: RangeError on Array#values_at with negative ranges
https://bugs.ruby-lang.org/issues/20957

* Author: kyanagi (Kouhei Yanagita)
* Status: Open
* ruby -v: ruby 3.4.0dev (2024-12-13T10:19:31Z master 3cb79d4082) +PRISM [arm64-darwin22]
* Backport: 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN
----------------------------------------
```
[0, 1, 2, 3].values_at(10)       #=> [nil]
[0, 1, 2, 3].values_at(10..10)   #=> [nil]
[0, 1, 2, 3].values_at(-10)      #=> [nil]
[0, 1, 2, 3].values_at(-10..-10) #=> 'Array#values_at': -10..-10 out of range (RangeError)
```

Is this the intended behavior?

I am aware that this behavior was recently added to the documentation, but I would like to confirm whether it is the intended behavior.




-- 
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/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [ruby-core:120631] [Ruby master Bug#20957] RangeError on Array#values_at with negative ranges
  2024-12-16  2:05 [ruby-core:120257] [Ruby master Bug#20957] RangeError on Array#values_at with negative ranges kyanagi (Kouhei Yanagita) via ruby-core
@ 2025-01-13 11:43 ` johnnyshields (Johnny Shields) via ruby-core
  0 siblings, 0 replies; 2+ messages in thread
From: johnnyshields (Johnny Shields) via ruby-core @ 2025-01-13 11:43 UTC (permalink / raw)
  To: ruby-core; +Cc: johnnyshields (Johnny Shields)

Issue #20957 has been updated by johnnyshields (Johnny Shields).


Here are more interesting cases:

```
# Reverse ranges don't return values
[0, 1, 2, 3].values_at(2..3) => [2, 3]
[0, 1, 2, 3].values_at(3..2) => []

# Positive values outside range return a nil for each index
[0, 1, 2, 3].values_at(3..10) => [3, nil, nil, nil, nil, nil, nil, nil]

# Negative ranges work so long as they are in ascending order and within the range
[0, 1, 2, 3].values_at(-4..-2)  #=> [0, 1, 2]
[0, 1, 2, 3].values_at(-5..-2)  #=> RangeError
[0, 1, 2, 3].values_at(-2..-4)  #=> []
[0, 1, 2, 3].values_at(-4..-10) #=> []
[0, 1, 2, 3].values_at(-10..-4) #=> RangeError
[0, 1, 2, 3].values_at(-5..-10) #=> RangeError

# Mix of negative and positive value does something sensible, so long as it doesn't create a "reverse range"
[0, 1, 2, 3].values_at(-4..1) #=> [0, 1]
[0, 1, 2, 3].values_at(-3..1) #=> [1]
[0, 1, 2, 3].values_at(-2..1) #=> []
[0, 1, 2, 3].values_at(-2..5) #=> [2, 3, nil, nil]
```

I would favor adding support for reverse ranges, and possibly the `nil, nil, nil` behavior for out-of-bounds negative ranges, to be congruent.

----------------------------------------
Bug #20957: RangeError on Array#values_at with negative ranges
https://bugs.ruby-lang.org/issues/20957#change-111460

* Author: kyanagi (Kouhei Yanagita)
* Status: Open
* ruby -v: ruby 3.4.0dev (2024-12-13T10:19:31Z master 3cb79d4082) +PRISM [arm64-darwin22]
* Backport: 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN
----------------------------------------
```
[0, 1, 2, 3].values_at(10)       #=> [nil]
[0, 1, 2, 3].values_at(10..10)   #=> [nil]
[0, 1, 2, 3].values_at(-10)      #=> [nil]
[0, 1, 2, 3].values_at(-10..-10) #=> 'Array#values_at': -10..-10 out of range (RangeError)
```

Is this the intended behavior?

I am aware that this behavior was recently added to the documentation, but I would like to confirm whether it is the intended behavior.




-- 
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/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2025-01-13 11:44 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-12-16  2:05 [ruby-core:120257] [Ruby master Bug#20957] RangeError on Array#values_at with negative ranges kyanagi (Kouhei Yanagita) via ruby-core
2025-01-13 11:43 ` [ruby-core:120631] " johnnyshields (Johnny Shields) via ruby-core

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).