From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: poffice@blade.nagaokaut.ac.jp Delivered-To: poffice@blade.nagaokaut.ac.jp Received: from kankan.nagaokaut.ac.jp (kankan.nagaokaut.ac.jp [133.44.2.24]) by blade.nagaokaut.ac.jp (Postfix) with ESMTP id 26FBB19C0061 for ; Tue, 10 Nov 2015 21:06:22 +0900 (JST) Received: from voscc.nagaokaut.ac.jp (voscc.nagaokaut.ac.jp [133.44.1.100]) by kankan.nagaokaut.ac.jp (Postfix) with ESMTP id C175AB5D882 for ; Tue, 10 Nov 2015 21:35:57 +0900 (JST) Received: from neon.ruby-lang.org (neon.ruby-lang.org [221.186.184.75]) by voscc.nagaokaut.ac.jp (Postfix) with ESMTP id 26A8318CC7D1 for ; Tue, 10 Nov 2015 21:35:57 +0900 (JST) Received: from [221.186.184.76] (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 960BE120489; Tue, 10 Nov 2015 21:35:53 +0900 (JST) X-Original-To: ruby-core@ruby-lang.org Delivered-To: ruby-core@ruby-lang.org Received: from o10.shared.sendgrid.net (o10.shared.sendgrid.net [173.193.132.135]) by neon.ruby-lang.org (Postfix) with ESMTPS id 701F8120436 for ; Tue, 10 Nov 2015 21:35:36 +0900 (JST) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sendgrid.me; h=from:to:references:subject:mime-version:content-type:content-transfer-encoding:list-id; s=smtpapi; bh=100FC+LdTVILt0phpnFkTeRTmEY=; b=Zqr4mgrD4aVAKbzc8D RPkfv6piHYk89Q/3w5apDDB+C2RKsQohklmLou+iOKOfcF/9vBbrPtEhjwot3TsO ++Cz16cp4usFGaRD3qttiFKurWYGHmfTeFSxRUHzijwpZt+uXgXwrSN/CNklcwut XNeJJHdvHqPRtUhQkXIITu2Ro= Received: by filter-355.sjc1.sendgrid.net with SMTP id filter-355.21863.5641E4654C 2015-11-10 12:34:45.651105634 +0000 UTC Received: from herokuapp.com (ec2-50-19-42-189.compute-1.amazonaws.com [50.19.42.189]) by ismtpd0006p1iad1.sendgrid.net (SG) with ESMTP id FpbEWOyfRcm0gbs2uheN_g for ; Tue, 10 Nov 2015 12:34:45.589 +0000 (UTC) Date: Tue, 10 Nov 2015 12:34:45 +0000 From: hanmac@gmx.de To: ruby-core@ruby-lang.org Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Redmine-MailingListIntegration-Message-Ids: 46071 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 11661 X-Redmine-Issue-Author: eddyluten X-Redmine-Issue-Assignee: matz X-Redmine-Sender: Hanmac 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: ync6xU2WACa70kv/Ymy4QrNMhiuLXJG8OTL2vJD1yS6tvgh+B0ki9Nh7nRzvgjGJCoNXcGAryvSHj3 DIN3EAPMZ2VePAE1PQMkNboZP/CcT/f6/YksCXLMIgnNu+c5kUa2c1uqwhfUTcKEvS49NlmmN5agHw uTMR9MT5JBDrwCg= X-SendGrid-Contentd-ID: {"test_id":"1447158887"} X-ML-Name: ruby-core X-Mail-Count: 71431 Subject: [ruby-core:71431] [Ruby trunk - Bug #11661] sprintf causes a KeyError instead of using a default value for hash substitution 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: , Errors-To: ruby-core-bounces@ruby-lang.org Sender: "ruby-core" Issue #11661 has been updated by Hans Mackowiak. hm might it be possible that sprintf only uses default value/or default proc if the hash does has default, but does still raise KeyError if it doesnt? another idea would be that is does try to use hash[key] function if hash is a non-hash object. (but that might reduce the calcing speed if not checked right) ---------------------------------------- Bug #11661: sprintf causes a KeyError instead of using a default value for hash substitution https://bugs.ruby-lang.org/issues/11661#change-54801 * Author: Eddy Luten * Status: Assigned * Priority: Normal * Assignee: Yukihiro Matsumoto * ruby -v: * Backport: 2.0.0: REQUIRED, 2.1: REQUIRED, 2.2: REQUIRED ---------------------------------------- When using a format string that substitutes hash values (or using the `%` operator on a string), instead of using the Hash's default value if a key is not present, it causes a KeyError. As an end-user, to get around this, my hash needs to know about all the possible keys ahead of time and pre-assign a value to them or handle the KeyError. Logically, I would assume that the `sprintf` implementation would use the default Hash value. I wanted to open this issue to see what your collective thoughts were on this since I have a fork running locally that fixes this issue and was wondering if I could send a patch/PR for this. This issue is reproducible using the following code: ```ruby my_hash = Hash.new('world') puts "hello %{location}" % my_hash # expecting "hello world" # "KeyError: key{location} not found" ``` -- https://bugs.ruby-lang.org/