# Hash merge

**URL:** <https://www.ruby-forum.com/t/hash-merge/137485>\
**Category:** Ruby\
**Created:** [May 13, 2008, 4:48pm UTC](https://www.ruby-forum.com/t/hash-merge/137485 "2008-05-13T16:48:44Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![twaters](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a6a055/32.png) [@twaters](https://www.ruby-forum.com/u/twaters)\
**Post date:** [May 13, 2008, 4:48pm UTC](https://www.ruby-forum.com/t/hash-merge/137485/1 "2008-05-13T16:48:44Z")

</div>

I’m not sure if I’m using this the right way and I’m hoping you guys can  
help.

I am merging two hashes together using merge and need to keep the keys  
the same and subtract the difference between the two values. The keys  
are account numbers and the values are the balances. Not sure why the  
balances are not subtracting and creating the new value.

Thanks in advance.  
Tim

class Calculate  
attr\_reader :sktyfuta, :sktyfutb  
def initialize(sktyfuta, sktyfutb)  
@sktyfuta = sktyfuta  
@sktyfutb = sktyfutb  
end

def data\_comp  
@sktyfuta.merge(@sktyfutb) { |key, old\_value, new\_value| old\_value

- new\_value }  
end  
end

---

<div class="post-metadata">

**Author:** ![twaters](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a6a055/32.png) [@twaters](https://www.ruby-forum.com/u/twaters)\
**Post date:** [May 13, 2008, 5:15pm UTC](https://www.ruby-forum.com/t/hash-merge/137485/2 "2008-05-13T17:15:18Z")

</div>

On May 13, 9:48 am, Tim W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## end end
> 
> Posted viahttp://www.ruby-forum.com/.

I’m not finding that to be the case.

> > a = {:a =\> 1, :b =\> 2}  
> > =\> {:a=\>1, :b=\>2}  
> > b = {:b =\> 0, :c =\> 3}  
> > =\> {:c=\>3, :b=\>0}  
> > a.merge b  
> > =\> {:c=\>3, :a=\>1, :b=\>0}  
> > a.merge(b) { |key, old, new| old - new }  
> > =\> {:c=\>3, :a=\>1, :b=\>2}

and with your code:

> > end  
> > =\> nil  
> > c = Calculate.new(a, b)  
> > =\> #\<Calculate:0x579cc @sktyfutb={:c=\>3, :b=\>0},  
> > @sktyfuta={:b=\>2, :a=\>1}\>  
> > c.data\_comp  
> > =\> {:c=\>3, :a=\>1, :b=\>2}

Thanks for teaching me about Hash#merge with a block, by the way.

---

<div class="post-metadata">

**Author:** ![twaters](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a6a055/32.png) [@twaters](https://www.ruby-forum.com/u/twaters)\
**Post date:** [May 13, 2008, 5:15pm UTC](https://www.ruby-forum.com/t/hash-merge/137485/3 "2008-05-13T17:15:39Z")

</div>

Tim W. wrote:

> I’m not sure if I’m using this the right way and I’m hoping you guys can  
> help.
> 
> I am merging two hashes together using merge and need to keep the keys  
> the same and subtract the difference between the two values. The keys  
> are account numbers and the values are the balances. Not sure why the  
> balances are not subtracting and creating the new value.
> 
> Thanks in advance.  
> Tim
> 
> class Calculate  
> attr\_reader :sktyfuta, :sktyfutb  
> def initialize(sktyfuta, sktyfutb)  
> @sktyfuta = sktyfuta  
> @sktyfutb = sktyfutb  
> end
> 
> def data\_comp  
> @sktyfuta.merge(@sktyfutb) { |key, old\_value, new\_value| old\_value
> 
> - new\_value }  
> end  
> end

Hash.merge will give you a new Hash, a third one. If somewhere outside  
of your class you call the method data\_comp, it will return this new  
Hash.

a = some\_instance\_of Calculate.datacomp  
p a

I guess you expect @sktyfuta to change, but it won’t unless you tell it  
to.

Regards,

Siep

---

<div class="post-metadata">

**Author:** ![twaters](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a6a055/32.png) [@twaters](https://www.ruby-forum.com/u/twaters)\
**Post date:** [May 13, 2008, 5:24pm UTC](https://www.ruby-forum.com/t/hash-merge/137485/4 "2008-05-13T17:24:21Z")

</div>

Thanks for the post guys. My problem is this:

a = { 54540 =\> 10345, 55550 =\> 30555 }  
b = { 54540 =\> 11000, 55550 =\> 40556 }

I need to merge these into one hash where the keys are the same so that  
is fine, I need to subtract the values and the difference will be the  
new key.

So I’m not sure this line is doing that I think it should do, I’m pretty  
sure I may have the wrong syntax.

@sktyfuta.merge(@sktyfutb) { |key, old\_value, new\_value| old\_value -  
new\_value }

Tim

---

<div class="post-metadata">

**Author:** ![twaters](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a6a055/32.png) [@twaters](https://www.ruby-forum.com/u/twaters)\
**Post date:** [May 13, 2008, 5:43pm UTC](https://www.ruby-forum.com/t/hash-merge/137485/5 "2008-05-13T17:43:49Z")

</div>

Tim W. wrote:

> Thanks for the post guys. My problem is this:
> 
> a = { 54540 =\> 10345, 55550 =\> 30555 }  
> b = { 54540 =\> 11000, 55550 =\> 40556 }
> 
> I need to merge these into one hash where the keys are the same so that  
> is fine, I need to subtract the values and the difference will be the  
> new key.  
> Are you sure? I’ll assume you meant value
> 
> So I’m not sure this line is doing that I think it should do, I’m pretty  
> sure I may have the wrong syntax.
> 
> @sktyfuta.merge(@sktyfutb) { |key, old\_value, new\_value| old\_value -  
> new\_value }
> 
> Tim

The syntax is fine,Yossef M. actually used your class. See his  
example. How do you expect it to behave?

regards,

Siep

---

<div class="post-metadata">

**Author:** ![twaters](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a6a055/32.png) [@twaters](https://www.ruby-forum.com/u/twaters)\
**Post date:** [May 13, 2008, 5:58pm UTC](https://www.ruby-forum.com/t/hash-merge/137485/6 "2008-05-13T17:58:01Z")

</div>

You guys were right it is working, I was returning the wrong values.  
Thanks for the help! I actual got something right 🙂

Thanks again  
Tim

> The syntax is fine,Yossef M. actually used your class. See his  
> example. How do you expect it to behave?
> 
> regards,
> 
> Siep

---

<div class="post-metadata">

**Author:** ![twaters](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a6a055/32.png) [@twaters](https://www.ruby-forum.com/u/twaters)\
**Post date:** [May 13, 2008, 6:52pm UTC](https://www.ruby-forum.com/t/hash-merge/137485/7 "2008-05-13T18:52:31Z")

</div>

On 5/13/08, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> irb(main):011:0\>
> 
> The block is simply ignored.

Not really:

irb(main):010:0\> h={1,2}  
=\> {1=\>2}  
irb(main):011:0\> h.merge(10=\>20){|\*a| p a}  
=\> {1=\>2, 10=\>20}  
irb(main):012:0\> h.merge(1=\>20){|\*a| p a}  
[1, 2, 20]  
=\> {1=\>nil}  
irb(main):013:0\>

The block is only called for matching keys.

-Adam

---

<div class="post-metadata">

**Author:** ![twaters](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a6a055/32.png) [@twaters](https://www.ruby-forum.com/u/twaters)\
**Post date:** [May 13, 2008, 6:21pm UTC](https://www.ruby-forum.com/t/hash-merge/137485/8 "2008-05-13T18:21:30Z")

</div>

On 13.05.2008 17:14, Yossef M. wrote:

> Thanks for teaching me about Hash#merge with a block, by the way.

Not really:

irb(main):009:0\> h = {1=\>2}  
=\> {1=\>2}  
irb(main):010:0\> h.merge(10=\>20) {|\*a| p a}  
=\> {1=\>2, 10=\>20}  
irb(main):011:0\>

The block is simply ignored.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![twaters](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a6a055/32.png) [@twaters](https://www.ruby-forum.com/u/twaters)\
**Post date:** [May 13, 2008, 11:36pm UTC](https://www.ruby-forum.com/t/hash-merge/137485/9 "2008-05-13T23:36:28Z")

</div>

On 13.05.2008 18:51, Adam S. wrote:

> > irb(main):011:0\>  
> > [1, 2, 20]  
> > =\> {1=\>nil}  
> > irb(main):013:0\>
> 
> The block is only called for matching keys.

Amazing. Thank you for the education! I should have looked at the  
documentation before making a fool of myself…

Kind regards

robert
