Issue #5553 has been updated by Yukihiro Matsumoto.

Status changed from Open to Feedback

???fetch???[]??????????????????????????????????????????????????????????????????????????????????if_key?????????????????????????????????????????????????????????????????????????????????????????????????????????????????? 
正?????????????????????????????????????????????????????人工?????????????????????????????????????????????????????????
----------------------------------------
Feature #5553: A method for Hash that works differently depending on whether a key exists
http://redmine.ruby-lang.org/issues/5553

Author: Tsuyoshi Sawada
Status: Feedback
Priority: Normal
Assignee: 
Category: 
Target version: 


A method Hash#if_key(key, [default], &pr) which works like the following will be often used, and is useful.

    a = {morning: "??????????", daytime: "???????????", evening: "?????????????", nothing: nil}
    a.if_key(:morning){|str| "#{str}????!"} #=> "??????????????!"
    a.if_key(:nothing){|str| "#{str}????!"} #=> "????!"
    a.if_key(:midnight){|str| "#{str}????!"} #=> nil
    a.if_key(:nothing, "???????"){|str| "#{str}????!"} #=> "???????"

That is, when `key' exists, then the corresponding value will be passed to `pr'. Otherwise, the given `default' or the implicit default will be returned.




-- 
http://redmine.ruby-lang.org