Hash diff function broken
Demuxx opened this issue · 8 comments
chef-stash/libraries/helper.rb:107 is currently failing during run because it starts with a period. I think you meant to do 'self.merge!' instead of '.merge!'.
Can you give more information about your Chef version, etc? Are you using the hook LWRP? Works fine via irb, but I don't use that specific LWRP.
$ irb
irb(main):001:0> class Hash
irb(main):002:1> # Returns a hash that represents the difference between two hashes.
irb(main):003:1* #
irb(main):004:1* # {1 => 2}.diff(1 => 2) # => {}
irb(main):005:1* # {1 => 2}.diff(1 => 3) # => {1 => 2}
irb(main):006:1* # {}.diff(1 => 2) # => {1 => 2}
irb(main):007:1* # {1 => 2, 3 => 4}.diff(1 => 2) # => {3 => 4}
irb(main):008:1* def diff(other)
irb(main):009:2> dup.delete_if { |k, v| other[k] == v }
irb(main):010:2> .merge!(other.dup.delete_if { |k, v| key?(k) })
irb(main):011:2> end
irb(main):012:1> end
=> nil
irb(main):013:0> {1 => 2}.diff(1 => 3)
=> {1=>2}
Also please pass along the full Chef error in a gist.
My original comment is the wrong fix. I'm using Vagrant as my chef solo environment, which uses 1.8.7, which in turn doesn't allow this type of syntax. The code as it's written compiles and runs fine with 2.0.0-current and 1.9.3. I'm not sure on the correct syntax to make it work on 1.8.7.
# Hash extension class
class Hash
# Returns a hash that represents the difference between two hashes.
#
# {1 => 2}.diff(1 => 2) # => {}
# {1 => 2}.diff(1 => 3) # => {1 => 2}
# {}.diff(1 => 2) # => {1 => 2}
# {1 => 2, 3 => 4}.diff(1 => 2) # => {3 => 4}
def diff(other)
d = dup.delete_if { |k, v| other[k] == v }
d.merge!(other.dup.delete_if { |k, v| key?(k) })
end
end
https://gist.github.com/Prandium/7034389 gist of the bug.
I'll patch in your fix. Thanks for the detective work.
Released in 3.0.1 - can you confirm the fix?
This should've been resolved in 3.0.1+ and hasn't been mentioned in 2+ weeks. Closing the issue. Let me know if its still a problem.