Comparing ruby hashes [duplicate]

here is a slightly modified version from colin’s. class Hash def diff(other) (self.keys + other.keys).uniq.inject({}) do |memo, key| unless self[key] == other[key] if self[key].kind_of?(Hash) && other[key].kind_of?(Hash) memo[key] = self[key].diff(other[key]) else memo[key] = [self[key], other[key]] end end memo end end end It recurses into the hashes for more efficient left and right {a: {c: 1, b: … Read more

How to find out if letter is Alphanumeric or Digit in Swift

For Swift 5 see rustylepord’s answer. Update for Swift 3: let letters = CharacterSet.letters let digits = CharacterSet.decimalDigits var letterCount = 0 var digitCount = 0 for uni in phrase.unicodeScalars { if letters.contains(uni) { letterCount += 1 } else if digits.contains(uni) { digitCount += 1 } } (Previous answer for older Swift versions) A possible … Read more

Compare arrays in swift

You’re right to be slightly nervous about ==: struct NeverEqual: Equatable { } func ==(lhs: NeverEqual, rhs: NeverEqual)->Bool { return false } let x = [NeverEqual()] var y = x x == y // this returns true [NeverEqual()] == [NeverEqual()] // false x == [NeverEqual()] // false let z = [NeverEqual()] x == z // … Read more

obj.nil? vs. obj == nil

Is it better to use obj.nil? or obj == nil It is exactly the same. It has the exact same observable effects from the outside ( pfff ) * and what are the benefits of both. If you like micro optimizations all the objects will return false to the .nil? message except for the object … Read more