From: Suraj Kurapati Date: 2011-07-27T02:52:02+09:00 Subject: [ruby-core:38527] [Ruby 1.9 - Feature #5008] Equal rights for Hash (like Array, String, Integer, Float) Issue #5008 has been updated by Suraj Kurapati. Yui NARUSE wrote: > First of all, Ruby has two way of the type conversion; implicit > and explicit. to_i, to_f, to_s, to_a and so on are explicit > conversion. to_int, to_str, to_ary and so on are implicit > conversion. I see, then for Hash: * to_h should be explicit conversion * to_hash should be implicit conversion. > > Add Kernel#Hash method for converting objects into Hash strictly > > (see issue #3131). > > Why don't you discuss in #3131? You're right. Sorry for bringing that up. > > Define NilClass#to_h so that we can convert nil into an empty > > Hash. > > You should show the use case: what is the benefit of the function. The benefit of NilClass#to_h is convenience. It is the same reason why NilClass#to_a and NilClass#to_s exist. For example, we might process an array that (1) can be empty or (2) have a hash as the first element: some_array.first.to_h.each_pair do |key, value| # do some processing ... end Without NilClass#to_h, we need to do some extra work: if hash = some_array.first hash.each_pair do |key, value| # do some processing ... end end Furthermore, this extra work seems unfair to Hash because all of the other primitive data structures (strings, arrays) have NilClass#to_* conversion methods. :-( Thanks for your consideration. ---------------------------------------- Feature #5008: Equal rights for Hash (like Array, String, Integer, Float) http://redmine.ruby-lang.org/issues/5008 Author: Suraj Kurapati Status: Assigned Priority: Normal Assignee: Yukihiro Matsumoto Category: Target version: =begin Hello, 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. =end -- http://redmine.ruby-lang.org