Hash merge

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

On May 13, 9:48 am, Tim W. [email protected] 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.

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

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

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

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

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

On 5/13/08, Robert K. [email protected] 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

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

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