[ruby-core:65171] [ruby-trunk - Feature #10270] [Open] Hash#insert

From: a7145@...
Date: 2014-09-20 13:25:34 UTC
List: ruby-core #65171
Issue #10270 has been reported by Atlas Prime.

----------------------------------------
Feature #10270: Hash#insert
https://bugs.ruby-lang.org/issues/10270

* Author: Atlas Prime
* Status: Open
* Priority: Normal
* Assignee: 
* Category: core
* Target version: 
----------------------------------------
Hash doesn't appear to have a "safe" way to insert an entry that won't clobber another key if it is already there. So how about:

~~~
class Hash

  # Like Hash#store but only stores if the key isn't already
  # in the hash. Returns true if inserted, otherwise false.
  #
  def insert(name, value)
    if key?(name)
      false
    else
      store(name,value)
      true
    end
  end

end
~~~




-- 
https://bugs.ruby-lang.org/

In This Thread

Prev Next