Project

General

Profile

Feature #4621

NilClass#to_hash

Added by sawa (Tsuyoshi Sawada) about 8 years ago. Updated about 8 years ago.

Status:
Rejected
Priority:
Normal
Target version:
-
[ruby-core:35923]

Description

=begin
Various methods defined on NilClass are handy to avoid Method undefined on NilClass errors. For example,

  • to_a for multiple dimensioned array when there might not be some values for some indices:

    array[i].to_a[j].to_a[k]
    
  • to_s for pattern match that might fail:

    string[regex].to_s*2
    
  • to_i, to_f, to_c for index search that might fail, etc:

    array.index(element).to_i*3
    

But there is no NilClass#to_hash even though there is Hash#to_hash. If such method existed (equivalent to class NilClass; def to_hash; {} end end), then we would be able to do:

  • to_hash for multiple embedded hash when there might not be some values for some keys:

    hash[:a].to_hash[:b].to_hash[:c]
    

Is there any reason why to_a, to_s, to_i, to_f, to_c are all defined on NilClass but not for to_hash? If there is not, I would like to request this feature.
=end

History

Updated by naruse (Yui NARUSE) about 8 years ago

  • Category set to core
  • Status changed from Open to Assigned
  • Assignee set to matz (Yukihiro Matsumoto)

=begin

=end

Updated by matz (Yukihiro Matsumoto) about 8 years ago

  • Status changed from Assigned to Rejected

=begin

=end

Updated by matz (Yukihiro Matsumoto) about 8 years ago

=begin
Hi,

In message "Re: [ruby-core:35924] [Ruby 1.9 - Feature #4621][Assigned] NilClass#to_hash"
on Wed, 27 Apr 2011 16:46:01 +0900, Yui NARUSE redmine@ruby-lang.org writes:

|Is there any reason why to_a, to_s, to_i, to_f, to_c are all defined on NilClass but not for to_hash? If there is not, I would like to request this feature.

to_a, etc. are explicit conversion methods, whereas to_int, to_hash,
etc. are implicit conversion methods. I don't see any good reason to
add implicit conversion methods to nil.

                        matz.

=end

Also available in: Atom PDF