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 46C4619C0061 for ; Wed, 11 Nov 2015 04:27:44 +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 C2F3DB5D8B1 for ; Wed, 11 Nov 2015 04:57:23 +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 BFD6418CC7F0 for ; Wed, 11 Nov 2015 04:57:23 +0900 (JST) Received: from [221.186.184.76] (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 199ED12047A; Wed, 11 Nov 2015 04:57:21 +0900 (JST) X-Original-To: ruby-core@ruby-lang.org Delivered-To: ruby-core@ruby-lang.org Received: from o2.heroku.sendgrid.net (o2.heroku.sendgrid.net [67.228.50.55]) by neon.ruby-lang.org (Postfix) with ESMTPS id 2BF3712045B for ; Wed, 11 Nov 2015 04:57:16 +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=SPLug4MMovvRIDh+M9PYTq3dsTQ=; b=aeZMNBydXbv5T3nNwF bzxI6Czv1RFM0ZXVUUkbODT59y31Oq+9or7hDfodOWdGjrpo6sDoX7LT6qCupTa+ uRGsA7ABRqVhoxgakQF1aMB2cLCQB1FlbVn6WNSOyNFv4wTq5zPGcN1cBEJPfKE4 WLQ2zBwrPVCdEEuUnwp7/gHBI= Received: by filter0602p1mdw1.sendgrid.net with SMTP id filter0602p1mdw1.19303.56424C0C43 2015-11-10 19:57:00.478037235 +0000 UTC Received: from herokuapp.com (ec2-54-166-59-15.compute-1.amazonaws.com [54.166.59.15]) by ismtpd0003p1iad1.sendgrid.net (SG) with ESMTP id yrzfIe20QjWSOVPd5ktKPQ Tue, 10 Nov 2015 19:57:00.531 +0000 (UTC) Date: Tue, 10 Nov 2015 19:57:00 +0000 From: eddyluten@gmail.com 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: 46074 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 11661 X-Redmine-Issue-Author: eddyluten X-Redmine-Issue-Assignee: matz X-Redmine-Sender: eddyluten 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/Ymy4QrNMhiuLXJG8OTL2vJD1yS6Sset8NQ3EbcqfC/SJbgKR4i/aXcgy29mf7u OFli8vF52XVeKrQ7h7LFeW8bQHuWoGnwmDUXiYgedLintlfyPBdDeXNaZ+uq3++BqSRCQqgYt9tvJB +UT50DMapRAiE33o73v7w5JcQWpvtRF4ZX1P X-SendGrid-Contentd-ID: {"test_id":"1447185428"} X-ML-Name: ruby-core X-Mail-Count: 71434 Subject: [ruby-core:71434] [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 Eddy Luten. Hans Mackowiak wrote: > 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) Nobuyoshi Nakada: this was the change I initially made myself but couldn't get it to work properly with hash substitutions. Maybe I did something wrong in my testing, I will attempt to use your changes, thanks! :) Hans Mackowiak: so raise KeyError is the default is `nil`? I think that could work, since if you wanted empty strings as a default, you could simply set an empty string as default value. However, logically, since `"#{nil}" (and `nil.to_s` for that case) works and returns an empty string, the modulus operator version of it should work the same. What do you think? Also a general question, how would I go about making the Rubyspec tests pass for this particular pull request? Do I open another PR for the tests in Rubyspec? If so, how do I link these two PRs to work together in the Travis CI build acceptance test? ---------------------------------------- Bug #11661: sprintf causes a KeyError instead of using a default value for hash substitution https://bugs.ruby-lang.org/issues/11661#change-54803 * 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/