Project

General

Profile

Actions

Bug #11661

closed

sprintf causes a KeyError instead of using a default value for hash substitution

Added by eddyluten (Eddy Luten) over 8 years ago. Updated over 8 years ago.

Status:
Closed
Target version:
-
[ruby-core:71354]

Description

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:

my_hash = Hash.new('world')
puts "hello %{location}" % my_hash # expecting "hello world"
# "KeyError: key{location} not found"

Related issues 1 (0 open1 closed)

Related to Ruby master - Bug #11677: 52530の変更により、sprintfの引数のhashにkeyが存在していてもvalueがnilのときにKeyErrorがでるようになってしまった。ClosedActions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0