[#44904] [ruby-trunk - Bug #5688][Open] Solaris10 で spawn を繰り返すとメモリリークする — okkez _ <redmine@...>

14 messages 2011/11/29

[ruby-dev:44779] [ruby-trunk - Feature #5553][Open] A method for Hash that works differently depending on whether a key exists

From: Tsuyoshi Sawada <sawadatsuyoshi@...>
Date: 2011-11-02 18:41:49 UTC
List: ruby-dev #44779
Issue #5553 has been reported by Tsuyoshi Sawada.

----------------------------------------
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: Open
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

In This Thread

Prev Next