From: BurdetteLamar Date: Tue, 22 Oct 2024 19:07:30 +0000 (-0500) Subject: [DOC] Tweaks for Array#to_h X-Git-Tag: v3_4_0_rc1~1090 X-Git-Url: https://repo.or.cz/ruby.git/commitdiff_plain/00af6a3f044692a36ff02355ec90c9593dddbaa9 [DOC] Tweaks for Array#to_h --- diff --git a/array.c b/array.c index f116cdd3f1..d5286803c6 100644 --- a/array.c +++ b/array.c @@ -3011,27 +3011,27 @@ rb_ary_to_a(VALUE ary) /* * call-seq: - * array.to_h -> new_hash - * array.to_h {|item| ... } -> new_hash + * to_h -> new_hash + * to_h {|element| ... } -> new_hash * - * Returns a new Hash formed from +self+. + * Returns a new hash formed from +self+. * - * When a block is given, calls the block with each array element; - * the block must return a 2-element +Array+ whose two elements - * form a key-value pair in the returned Hash: + * With no block given, each element of +self+ must be a 2-element sub-array; + * forms each sub-array into a key-value pair in the new hash: * - * a = ['foo', :bar, 1, [2, 3], {baz: 4}] - * h = a.to_h {|item| [item, item] } - * h # => {"foo"=>"foo", :bar=>:bar, 1=>1, [2, 3]=>[2, 3], {:baz=>4}=>{:baz=>4}} + * a = [['foo', 'zero'], ['bar', 'one'], ['baz', 'two']] + * a.to_h # => {"foo"=>"zero", "bar"=>"one", "baz"=>"two"} + * [].to_h # => {} * - * When no block is given, +self+ must be an +Array+ of 2-element sub-arrays, - * each sub-array is formed into a key-value pair in the new Hash: + * With a block given, the block must return a 2-element array; + * calls the block with each element of +self+; + * forms each returned array into a key-value pair in the returned hash: * - * [].to_h # => {} - * a = [['foo', 'zero'], ['bar', 'one'], ['baz', 'two']] - * h = a.to_h - * h # => {"foo"=>"zero", "bar"=>"one", "baz"=>"two"} + * a = ['foo', :bar, 1, [2, 3], {baz: 4}] + * a.to_h {|element| [element, element.class] } + * # => {"foo"=>String, :bar=>Symbol, 1=>Integer, [2, 3]=>Array, {:baz=>4}=>Hash} * + * Related: see {Methods for Converting}[rdoc-ref:Array@Methods+for+Converting]. */ static VALUE