Feature #5008

Equal rights for Hash (like Array, String, Integer, Float)

Added by sunaku (Suraj Kurapati) over 9 years ago. Updated over 7 years ago.

Target version:



I am using ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-linux].

Although Ruby has a rich set of primitive data types and structures,
the Hash seems neglected in the Ruby API in comparison to its peers:

  • String: Object#to_s by API
  • Integer: Kernel#Integer by API and Object#to_i by convention
  • Float: Kernel#Float by API and Object#to_f by convention
  • Array: Kernel#Array by API and Object#to_a by convention
  • Hash: Kernel#Hash (issue #3131) and Object#to_hash by convention

In particular, the Hash seems neglected by the Ruby API because:

  • Its convention method (#to_hash) is longer than one character (#to_h).
  • It did not have a Kernel-level method until recently (see issue #3131).
  • It has no methods for conversion from NilClass, unlike #to_s, a, i, f.

Please rectify this un-orthogonality and grant Hash equal rights by:

  • Establish #to_h as the convention method for converting objects into Hash.
  • Add Kernel#Hash method for converting objects into Hash strictly (see issue #3131).
  • Define NilClass#to_h so that we can convert nil into an empty Hash.

Thanks for your consideration.

Related issues

Related to Ruby master - Feature #4151: Enumerable#categorizeRejectedakr (Akira Tanaka)Actions
Related to Ruby master - Feature #7241: Enumerable#to_h proposalRejected10/30/2012Actions
Related to Ruby master - Feature #6276: to_h as explicit conversion to HashClosedmarcandre (Marc-Andre Lafortune)04/10/2012Actions
Is duplicate of Ruby master - Feature #4862: Struct#to_hashRejectedmatz (Yukihiro Matsumoto)06/10/2011Actions
Is duplicate of Ruby master - Feature #1400: Please add a method to enumerate fields in OpenStructClosedmarcandre (Marc-Andre Lafortune)04/23/2009Actions
Precedes Ruby master - Feature #3131: add Kernel#Hash() method like Kernel#Array()Closedmatz (Yukihiro Matsumoto)07/11/201107/11/2011Actions

Also available in: Atom PDF