From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-Status: No, score=-3.8 required=3.0 tests=AWL,BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,UNPARSEABLE_RELAY shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from neon.ruby-lang.org (neon.ruby-lang.org [221.186.184.75]) by dcvr.yhbt.net (Postfix) with ESMTP id AB2551F55B for ; Sat, 23 May 2020 02:26:11 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 696CB120995; Sat, 23 May 2020 11:25:46 +0900 (JST) Received: from o1678948x4.outbound-mail.sendgrid.net (o1678948x4.outbound-mail.sendgrid.net [167.89.48.4]) by neon.ruby-lang.org (Postfix) with ESMTPS id 37F35120977 for ; Sat, 23 May 2020 11:25:44 +0900 (JST) Received: by filterdrecv-p3iad2-8ddf98858-w5zgs with SMTP id filterdrecv-p3iad2-8ddf98858-w5zgs-17-5EC889BC-43 2020-05-23 02:26:04.873001404 +0000 UTC m=+5015919.167329014 Received: from herokuapp.com (unknown) by ismtpd0039p1mdw1.sendgrid.net (SG) with ESMTP id OJfaB7uQQnGb2a5GB8-uDg for ; Sat, 23 May 2020 02:26:04.738 +0000 (UTC) Date: Sat, 23 May 2020 02:26:04 +0000 (UTC) From: merch-redmine@jeremyevans.net Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 74279 X-Redmine-Project: ruby-master X-Redmine-Issue-Tracker: Bug X-Redmine-Issue-Id: 16908 X-Redmine-Issue-Author: ioquatix X-Redmine-Sender: jeremyevans0 X-Mailer: Redmine X-Redmine-Host: bugs.ruby-lang.org X-Redmine-Site: Ruby Issue Tracking System X-Auto-Response-Suppress: All Auto-Submitted: auto-generated X-SG-EID: =?us-ascii?Q?RVE3t853K5scBhbmJHUzZTFFeVC=2FZSUmHZ0Dc+26wcEi2CTgsF1oz0wTSSxGGN?= =?us-ascii?Q?BIPvCOrJo70Mm66H9I0vbOOyeTsOYPqHjCWGqn7?= =?us-ascii?Q?cEPdVHaykzZBGW58zARKmdG2yba6=2Fk1=2FikdJCNx?= =?us-ascii?Q?iLGqsWUNFLy5OVuSl0LoF0cRvfsvRuRUFz72BwP?= =?us-ascii?Q?GoCSih91hXQ2DpFZS6IXJry2SI6RrLZI6AW5Leh?= =?us-ascii?Q?H10OS3PNkM2bp4fbM=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 98487 Subject: [ruby-core:98487] [Ruby master Bug#16908] Strange behaviour of Hash#shift when used with `default_proc`. X-BeenThere: ruby-core@ruby-lang.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: Ruby developers List-Id: Ruby developers List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ruby-core-bounces@ruby-lang.org Sender: "ruby-core" Issue #16908 has been updated by jeremyevans0 (Jeremy Evans). While your particular example is non-intuitive, there is a simple explanation for it. The first time `hash.shift` is called, `hash` is empty, so it returns the default value (`0`). It gets the default value by calling the default_proc for `hash` with a `nil` key. There is no better option since `hash.shift` isn't provided a key. The second time `hash.shift` is called, the hash is not empty, so it returns the first entry as a key value pair. I agree `Hash#shift` semantics with a default_proc are questionable, but I'm not sure if it could be improved. I don't think we should change this behavior. It is expected that `Hash.new.shift` should return nil, as should `Hash.new(nil).shift` and `Hash.new{}.shift`. `hash.shift` is used in conditionals: ```ruby hash = {a: 1, b: 2} while (k,v = hash.shift) p [k, v] end ``` If you change `Hash#shift` to return an array when the hash is empty, you've turned this into an infinite loop. ---------------------------------------- Bug #16908: Strange behaviour of Hash#shift when used with `default_proc`. https://bugs.ruby-lang.org/issues/16908#change-85763 * Author: ioquatix (Samuel Williams) * Status: Open * Priority: Normal * ruby -v: 2.7.0 * Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN ---------------------------------------- I don't have any strong opinion about this, but I observed the following behaviour which I thought was confusing. Maybe it's okay, or maybe we should change it to be more consistent. ``` hash = Hash.new{|k,v| k[v] = 0} hash.shift # => 0 hash.shift # => [nil, 0] ``` My feeling was, both cases should return `[nil, 0]`. -- https://bugs.ruby-lang.org/