-
-
Save 0x0dea/db912a66e41b9b62859e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/hash.c b/hash.c | |
index 3cdc75e..ef2acd7 100644 | |
--- a/hash.c | |
+++ b/hash.c | |
@@ -888,7 +888,7 @@ rb_hash_fetch(VALUE hash, VALUE key) | |
* h.default(2) #=> 20 | |
*/ | |
-static VALUE | |
+VALUE | |
rb_hash_default(int argc, VALUE *argv, VALUE hash) | |
{ | |
VALUE key, ifnone; | |
diff --git a/include/ruby/intern.h b/include/ruby/intern.h | |
index 56865f3..0be1aee 100644 | |
--- a/include/ruby/intern.h | |
+++ b/include/ruby/intern.h | |
@@ -521,6 +521,7 @@ VALUE rb_hash_clear(VALUE); | |
VALUE rb_hash_delete_if(VALUE); | |
VALUE rb_hash_delete(VALUE,VALUE); | |
VALUE rb_hash_set_ifnone(VALUE hash, VALUE ifnone); | |
+VALUE rb_hash_default(int argc, VALUE *argv, VALUE hash); | |
typedef VALUE rb_hash_update_func(VALUE newkey, VALUE oldkey, VALUE value); | |
VALUE rb_hash_update_by(VALUE hash1, VALUE hash2, rb_hash_update_func *func); | |
struct st_table *rb_hash_tbl(VALUE); | |
diff --git a/sprintf.c b/sprintf.c | |
index 4549791..567cad4 100644 | |
--- a/sprintf.c | |
+++ b/sprintf.c | |
@@ -610,7 +610,12 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt) | |
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); | |
+ if (rb_hash_default(0, NULL, hash) != Qnil || FL_TEST(hash, HASH_PROC_DEFAULT)) { | |
+ nextvalue = rb_hash_aref(hash, sym); | |
+ } | |
+ else { | |
+ rb_enc_raise(enc, rb_eKeyError, "key%.*s not found", len, start); | |
+ } | |
} | |
if (term == '}') goto format_s; | |
p++; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment