ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: nobu@ruby-lang.org
To: ruby-core@ruby-lang.org
Subject: [ruby-core:71386] [Ruby trunk - Feature #11661] [Feedback] sprintf causes a KeyError instead of using a default value for hash substitution
Date: Sun, 08 Nov 2015 07:58:50 +0000	[thread overview]
Message-ID: <redmine.journal-54757.20151108075850.246bd11965bb2d5f@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-11661.20151105204817@ruby-lang.org

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/

  parent reply	other threads:[~2015-11-08  7:29 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <redmine.issue-11661.20151105204817@ruby-lang.org>
2015-11-05 20:48 ` [ruby-core:71354] [Ruby trunk - Bug #11661] [Open] sprintf causes a KeyError instead of using a default value for hash substitution eddyluten
2015-11-08  7:58 ` nobu [this message]
2015-11-08  8:36 ` [ruby-core:71387] [Ruby trunk - Feature #11661] [Assigned] " nobu
2015-11-10 11:37 ` [ruby-core:71430] [Ruby trunk - Bug #11661] " nobu
2015-11-10 12:34 ` [ruby-core:71431] " hanmac
2015-11-10 19:57 ` [ruby-core:71434] " eddyluten
2015-11-10 23:21 ` [ruby-core:71436] " nobu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-54757.20151108075850.246bd11965bb2d5f@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).