summaryrefslogtreecommitdiff
path: root/enum.c
diff options
context:
space:
mode:
authornaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-12-23 07:12:32 +0000
committernaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-12-23 07:12:32 +0000
commit0f7e52f9902a575cf76e63126f11793985dcf29e (patch)
treecfa88913ea11656da34508f212ecccbbc5fa4115 /enum.c
parentb6a2e6aa51c0ce34f0d5f41b617f312b03a07549 (diff)
merge revision(s) 44354: [Backport #9270]
* array.c: Have to_h raise on elements that are not key-value pairs [#9239] * enum.c: ditto git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_1@44361 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'enum.c')
-rw-r--r--enum.c16
1 files changed, 11 insertions, 5 deletions
diff --git a/enum.c b/enum.c
index 6e40b50c4d..e37ff16c5d 100644
--- a/enum.c
+++ b/enum.c
@@ -512,12 +512,19 @@ enum_to_a(int argc, VALUE *argv, VALUE obj)
static VALUE
enum_to_h_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, hash))
{
+ VALUE key_value_pair;
ENUM_WANT_SVALUE();
rb_thread_check_ints();
- i = rb_check_array_type(i);
- if (!NIL_P(i) && RARRAY_LEN(i) == 2) {
- rb_hash_aset(hash, RARRAY_AREF(i, 0), RARRAY_AREF(i, 1));
+ key_value_pair = rb_check_array_type(i);
+ if (NIL_P(key_value_pair)) {
+ rb_raise(rb_eTypeError, "wrong element type %s (expected array)",
+ rb_builtin_class_name(i));
}
+ if (RARRAY_LEN(key_value_pair) != 2) {
+ rb_raise(rb_eArgError, "element has wrong array length (expected 2, was %ld)",
+ RARRAY_LEN(key_value_pair));
+ }
+ rb_hash_aset(hash, RARRAY_AREF(key_value_pair, 0), RARRAY_AREF(key_value_pair, 1));
return Qnil;
}
@@ -526,8 +533,7 @@ enum_to_h_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, hash))
* enum.to_h(*args) -> hash
*
* Returns the result of interpreting <i>enum</i> as a list of
- * <tt>[key, value]</tt> pairs. Elements other than pairs of
- * values are ignored.
+ * <tt>[key, value]</tt> pairs.
*
* %i[hello world].each_with_index.to_h
* # => {:hello => 0, :world => 1}