Bug #1385

Wonderful undocumented feature in Ruby 1.8.7 & 1.9

Added by marcandre (Marc-Andre Lafortune) over 11 years ago. Updated over 9 years ago.

Target version:
ruby -v:
ruby 1.9.1p0 (2009-01-30 revision 21907) [i386-darwin9.6.0]


6 months ago, I begged for a natural way to construct a Hash from key-value pairs:
Maybe because of a lack of superstitious devil worshippers, there was no additional comment on this issue # 666, which seems even stranger after a discovery I made by pure random luck (hey, my last name is not Lafortune for nothing!). It appears there already is a way, using Hash::[]

Hash[[[:i_like, :ruby], [:hello, "world!"], [:answer, 42]]]
=> {:i_like => :ruby, :hello => "world!", :answer => 42}

Wouhou, exciting! This works in both Ruby 1.8.7 and 1.9.1, but it is not documented anywhere where it should:
The Ruby Programming Language (section

At first I thought maybe my satanic incantations were to thank for this hidden feature, but the huge 1.9 changelog reads:

Fri Oct 26 01:48:28 2007 Yukihiro Matsumoto

* hash.c (rb_hash_s_create): Hash#[] now takes assocs as source of
  hash conversion.

So, thank you Matz!

I'm thus filing a bug report for the missing documentation, praying that the reason it is not documented is a simple oversight and that we can officially count on this for the future. In any case I eagerly included this cool feature in my backports for 1.8.x ( ) since I still don't get the point of 1.8.7 ( ) and I'll use Hash::[] until we get (one day maybe) my dreamed Enumerable#to_hash which I still favor.

I'd propose an updated doc similar to the following:


  • call-seq:
  • Hash[ key, value, ... ] => hash
  • Hash[ [ [key, value], ... ] ] => hash
  • Hash[ object ] => hash *
  • Creates a new hash populated with the given objects. Equivalent to
  • the literal { key => value, ... }. In the first
  • form, keys and values occur in pairs, so there must be an even number of arguments.
  • The second and third form take a single argument which is either
  • an array of key-value pairs or an object convertible to a hash. *
  • Hash["a", 100, "b", 200] #=> {"a"=>100, "b"=>200}
  • Hash[ [ ["a" => 100], ["b" => 200] ] ] #=> {"a"=>100, "b"=>200}
  • Hash["a" => 100, "b" => 200] #=> {"a"=>100, "b"=>200}
  • {"a" => 100, "b" => 200} #=> {"a"=>100, "b"=>200} */

Is it just me or is the last example not quite insightful? In any case, if a different text than mine is used, note that the present version still mentions the old 1.8 syntax: { key, value, ... }

Is there a prize for the most long-winded bug report just for the documentation? To be a sure winner, I'll add that I find interesting that my infamous feature request # 666 would make the second form merge naturally into the third, since an array of key-value pairs would be convertible to a hash!

Thank you for your attention,

Marc-André Lafortune


doc_hash.patch (1.27 KB) doc_hash.patch marcandre (Marc-Andre Lafortune), 06/20/2009 08:51 PM

Related issues

Related to Ruby master - Bug #5406: inconsistent odd-length array rejection in Hash[*ary] vs Hash[ary]Closednobu (Nobuyoshi Nakada)10/05/2011Actions
Related to Ruby 1.8 - Bug #1000: Segmentation fault in Hash#[]Closed01/11/2009Actions

Also available in: Atom PDF