hash merge

T

Tim Wolak

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
 
Y

Yossef Mendelssohn

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

I'm not finding that to be the case.
=> {:c=>3, :a=>1, :b=>2}

and with your code:
=> {:c=>3, :a=>1, :b=>2}

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

Siep Korteling

Tim said:
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
 
T

Tim Wolak

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
 
S

Siep Korteling

Tim said:
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 Mendelssohn actually used your class. See his
example. How do you expect it to behave?

regards,

Siep
 
T

Tim Wolak

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
 
R

Robert Klemme

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
 
A

Adam Shelly

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.
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
 
R

Robert Klemme

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.
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.

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

Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top