# Hash#+?

**URL:** <https://rubytalk.org/t/hash/4276>\
**Category:** ruby-talk\
**Created:** [23 January 2003 01:45 UTC](https://rubytalk.org/t/hash/4276 "2003-01-23T01:45:20Z")\
**Posts on this page:** 2\
**Page:** 2

<div class="post-metadata">

**Author:** ![Tom\_Sawyer](https://avatars.discourse-cdn.com/v4/letter/t/a587f6/32.png) [@Tom\_Sawyer](https://rubytalk.org/u/Tom_Sawyer)\
**Post date:** [3 February 2003 13:30 UTC](https://rubytalk.org/t/hash/4276/21 "2003-02-03T13:30:00Z")

</div>

> We have live examples that show that Array#+ is useful. Try grepping  
> the source tree for /+=? \*[/. Another reason for Array#+ is that  
> arrays are stackable and a + operator matches our expectation and an  
> analogy to String#+.

well, that’s kind of unfair in that i can’t do the same grep on Hash#|. if it  
existed then it would be different story. although i’m sure Array#+ would get  
used more. i don’t dispute that.

also i’m not in favor of Hash#+ for the reasons you state. i believe it shoud  
be Hash#| since duplicates are overwirtten, which is more in line with with  
Array#| where duplicates are removed.

> My question is: would it be so a popular demand to add a new method  
> for Hash? We have been living without it and a.dup.update(b) or  
> {}.update(a).update(b) will do the job if you need the functionality.  
> I’m not really against the idea, but just curious as to how it is  
> going to be used.

the utility that i’ve come across is when i want to keep my original hash but  
also make a new one with addional elements. guess that’s obvious. like i  
said, i will give real examples as they come up. (there’s a couple in the app  
i’m working on, but it is a huge cgi app, and i don’t recall off hand where  
they were.) but i’ll let you know.

> **···**
>
> On Monday 03 February 2003 06:06 am, Akinori MUSHA wrote:
> 
> > At Mon, 3 Feb 2003 21:31:53 +0900, \> \> Tom Sawyer wrote:
> 
> –  
> tom sawyer, aka transami  
> [transami@transami.net](mailto:transami@transami.net)

---

<div class="post-metadata">

**Author:** ![Mark\_Slagell](https://avatars.discourse-cdn.com/v4/letter/m/87869e/32.png) [@Mark\_Slagell](https://rubytalk.org/u/Mark_Slagell)\
**Post date:** [3 February 2003 13:55 UTC](https://rubytalk.org/t/hash/4276/22 "2003-02-03T13:55:45Z")

</div>

Akinori MUSHA wrote:

> > > > What’s wrong with ‘hash3 = hash1.dup.update(hash2)’ ? And I’d like to  
> > > > know how a non in-place version of update is used that frequently.  
> > > > Would you show us some (live) examples?
> > 
> > by the way, whats wrong with ‘arr3 = arr1.dup.concat(arr2)’ ?
> 
> We have live examples that show that Array#+ is useful. Try grepping  
> the source tree for /+=? \*[/. Another reason for Array#+ is that  
> arrays are stackable and a + operator matches our expectation and an  
> analogy to String#+.
> 
> My question is: would it be so a popular demand to add a new method  
> for Hash? We have been living without it and a.dup.update(b) or  
> {}.update(a).update(b) will do the job if you need the functionality.  
> I’m not really against the idea, but just curious as to how it is  
> going to be used.

I’ve argued before that this is a natural thing to want when using  
keyword arguments, for instance if there are oft-used packages of args  
that you want to repeatedly pass with small variations.

Without Hash#+ or Hash#|, one possibly useful idiom might be for methods  
that accepts a hash of args to accept two hashes instead, with the  
second defaulting to empty, and with the “dup.update” bit hidden from  
the caller. Just thought of that. But I’d still prefer to settle on a  
concise name for non-destructive hash update.

Mark

> **···**
>
> > At Mon, 3 Feb 2003 21:31:53 +0900, \>Tom Sawyer wrote:
> > 
> > > > On Sunday 02 February 2003 11:25 pm, Akinori MUSHA wrote:

[Previous page](https://rubytalk.org/t/hash/4276.md?page=1)
