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 362AF19C0059 for ; Sun, 8 Nov 2015 16:29:40 +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 32615B5D8E4 for ; Sun, 8 Nov 2015 16:58:59 +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 6521318CC7D1 for ; Sun, 8 Nov 2015 16:58:59 +0900 (JST) Received: from [221.186.184.76] (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id A1A36120465; Sun, 8 Nov 2015 16:58:57 +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 ADE3412045C for ; Sun, 8 Nov 2015 16:58:54 +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=Sm664ZrsDCn8hDfEkQtk6iCkOvA=; b=CbeBfXf/rQqCK0yYbn NWLHtN8ob4FPKFgbN3P+O3PzA2IuSPS5Um9l0iYL+jCoA3NR5cKWGzYJAfb4U7C2 4QW4pOdjRaG7VvuWpZwiTpgBhPJqTMEbVZpDUZGo3yl8FKmH4RAEgFesJ0BRizBC SafFjsP044nclb5bZXRZBvl1g= Received: by filter-136.sjc1.sendgrid.net with SMTP id filter-136.30930.563F00BA33 2015-11-08 07:58:50.965021041 +0000 UTC Received: from herokuapp.com (ec2-54-161-173-159.compute-1.amazonaws.com [54.161.173.159]) by ismtpd0003p1iad1.sendgrid.net (SG) with ESMTP id gTfCnBwOR4K2W3ypJKbX-w for ; Sun, 08 Nov 2015 07:58:50.896 +0000 (UTC) Date: Sun, 08 Nov 2015 07:58:50 +0000 From: nobu@ruby-lang.org 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: 46029 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 11661 X-Redmine-Issue-Author: eddyluten X-Redmine-Issue-Assignee: matz X-Redmine-Sender: nobu 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/Ymy4QrNMhiuLXJG8OTL2vJD1yS42bzV5SVhq7aXdy+lUSFUSvJEj+aholtVnrd 0cqMVqxIMAMBzf777vHeCg8q6XyHCw3j/qW/gs3/JzRvEuF73V8hv3e7ZH+XyqVN4mP3dfHxmDg2c5 rc96qcKwjv3jKwY= X-ML-Name: ruby-core X-Mail-Count: 71386 Subject: [ruby-core:71386] [Ruby trunk - Feature #11661] [Feedback] 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 Nobuyoshi Nakada. Tracker changed from Bug to Feature Status changed from Open to Feedback [GH-1087](https://github.com/ruby/ruby/pull/1087) doesn't work properly with a default proc expecting an argument, but segfaults. My patch. ~~~diff diff --git i/sprintf.c w/sprintf.c index 705d3ce..eee5695 100644 --- i/sprintf.c +++ w/sprintf.c @@ -605,13 +605,10 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt) } CHECKNAMEARG(start, len, enc); get_hash(&hash, argc, argv); - sym = rb_check_symbol_cstr(start + 1, - len - 2 /* without parenthesis */, - enc); - if (sym != Qnil) nextvalue = rb_hash_lookup2(hash, sym, Qundef); - if (nextvalue == Qundef) { - rb_enc_raise(enc, rb_eKeyError, "key%.*s not found", len, start); - } + sym = rb_cstr_intern(start + 1, + len - 2 /* without parenthesis */, + enc); + nextvalue = rb_hash_aref(hash, sym); if (term == '}') goto format_s; p++; goto retry; ~~~ ---------------------------------------- Feature #11661: sprintf causes a KeyError instead of using a default value for hash substitution https://bugs.ruby-lang.org/issues/11661#change-54757 * Author: Eddy Luten * Status: Feedback * Priority: Normal * Assignee: Yukihiro Matsumoto ---------------------------------------- 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/