Project

General

Profile

Bug #15443

Hash#slide with infinite range gives unexpective result

Added by ana06 (Ana Maria Martinez Gomez) 11 months ago. Updated 11 months ago.

Status:
Rejected
Priority:
Normal
Assignee:
-
Target version:
-
ruby -v:
trunk 2.6
[ruby-core:90643]

Description

{ 1 => :a, 2 => :b, 3 => :c }.slice(2..) returns {}. I had expected that it retuns {2=>:b, 3=>:c} instead.

History

Updated by Hanmac (Hans Mackowiak) 11 months ago

Hash#slice takes an array of keys via *keys

each parameter is checked as a key in the Hash

 { 1 => :a, 2 => :b, 3 => :c }.slice(2, 3) #=>  { 2 => :b, 3 => :c }
 { 1 => :a, 2 => :b, 3 => :c }.slice([2, 3]) #=>  {}  [2,3] is not a key in Hash

There is no special Rule for Range, that's why you get {}

Updated by mame (Yusuke Endoh) 11 months ago

  • Status changed from Open to Rejected

Not a bug but a spec of Hash#slice. Consider the case where the keys are a range:

{ (1..) => :a, (2..) => :b, (3..) => :c }.slice(2..) #=> {2..=>:b}

Note that this behavior is not specific to endless range:

{ 1 => :a, 2 => :b, 3 => :c }.slice(2..3) #=> {}

Updated by ana06 (Ana Maria Martinez Gomez) 11 months ago

It sound reasonable ;) this can be closed then

Also available in: Atom PDF