Skip to content
This repository has been archived by the owner on Jan 10, 2023. It is now read-only.

Hash diff function broken #13

Closed
Demuxx opened this issue Oct 17, 2013 · 8 comments
Closed

Hash diff function broken #13

Demuxx opened this issue Oct 17, 2013 · 8 comments

Comments

@Demuxx
Copy link

Demuxx commented Oct 17, 2013

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

@bflad
Copy link
Owner

bflad commented Oct 17, 2013

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}

@bflad
Copy link
Owner

bflad commented Oct 17, 2013

Also please pass along the full Chef error in a gist.

@Demuxx
Copy link
Author

Demuxx commented Oct 17, 2013

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.

@Demuxx
Copy link
Author

Demuxx commented Oct 17, 2013

# 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

@Demuxx
Copy link
Author

Demuxx commented Oct 17, 2013

@bflad
Copy link
Owner

bflad commented Oct 19, 2013

I'll patch in your fix. Thanks for the detective work.

@bflad
Copy link
Owner

bflad commented Oct 19, 2013

Released in 3.0.1 - can you confirm the fix?

@bflad
Copy link
Owner

bflad commented Nov 4, 2013

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.

@bflad bflad closed this as completed Nov 4, 2013
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants