summaryrefslogtreecommitdiff
path: root/array.c
diff options
context:
space:
mode:
authormarcandre <marcandre@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-12-23 03:42:29 +0000
committermarcandre <marcandre@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-12-23 03:42:29 +0000
commit9471f4187f104f7a0942fdccc884e57cf8027d07 (patch)
tree313b0a21a768e3d949de8f3e567bc5fe87bea811 /array.c
parent573762a7b7bbaf08aa1c27d95a9bcb15bac9ee2e (diff)
* 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/trunk@44354 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'array.c')
-rw-r--r--array.c13
1 files changed, 9 insertions, 4 deletions
diff --git a/array.c b/array.c
index 0241d72b5f..45713c55b0 100644
--- a/array.c
+++ b/array.c
@@ -2130,8 +2130,7 @@ rb_ary_to_a(VALUE ary)
* ary.to_h -> hash
*
* Returns the result of interpreting <i>ary</i> as an array of
- * <tt>[key, value]</tt> pairs. Elements other than pairs of
- * values are ignored.
+ * <tt>[key, value]</tt> pairs.
*
* [[:foo, :bar], [1, 2]].to_h
* # => {:foo => :bar, 1 => 2}
@@ -2144,9 +2143,15 @@ rb_ary_to_h(VALUE ary)
VALUE hash = rb_hash_new();
for (i=0; i<RARRAY_LEN(ary); i++) {
VALUE key_value_pair = rb_check_array_type(rb_ary_elt(ary, i));
- if (!NIL_P(key_value_pair) && (RARRAY_LEN(key_value_pair) == 2)) {
- rb_hash_aset(hash, RARRAY_AREF(key_value_pair, 0), RARRAY_AREF(key_value_pair, 1));
+ if (NIL_P(key_value_pair)) {
+ rb_raise(rb_eTypeError, "wrong element type %s at %ld (expected array)",
+ rb_builtin_class_name(rb_ary_elt(ary, i)), i);
}
+ if (RARRAY_LEN(key_value_pair) != 2) {
+ rb_raise(rb_eArgError, "wrong array length at %ld (expected 2, was %ld)",
+ i, RARRAY_LEN(key_value_pair));
+ }
+ rb_hash_aset(hash, RARRAY_AREF(key_value_pair, 0), RARRAY_AREF(key_value_pair, 1));
}
return hash;
}