Skip to content

Instantly share code, notes, and snippets.

@drewblas
Created February 13, 2013 16:14
Show Gist options
  • Save drewblas/4945697 to your computer and use it in GitHub Desktop.
Save drewblas/4945697 to your computer and use it in GitHub Desktop.

Revisions

  1. drewblas created this gist Feb 13, 2013.
    49 changes: 49 additions & 0 deletions ruby_quiz1.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,49 @@
    # =============================
    # Test case showing what we expect to happen
    a1 = 'z'
    b1 = 'z'
    a = [a1]
    b = [b1]

    a1 == b1 # true
    a1.eql? b1 # true
    a1.equal? b1 # false different object_ids, as expected
    a.eql? b # true

    a - b # = []
    b - a # = []
    #EVERYTHING IS GOOD HERE. DIFFERENT OBJECT_IDS BUT EQUAL OBJECTS SUBTRACT OUT OF ARRAY

    # =============================
    # Simple class that should act the same as above
    class MyObject
    attr_accessor :str
    def initialize(str)
    @str = str
    end

    def eql?(other)
    @str.eql? other.str
    end

    def ==(other)
    @str == other.str
    end
    end

    # =============================
    # Test case that is messed up!
    object1 = MyObject.new('z')
    object2 = MyObject.new('z')
    a = [object1]
    b = [object2]

    object1 == object2 # true
    object1.eql? object2 # true
    object1.equal? object2 # false different object_ids, as expected
    a.eql? b # true

    a - b # = [object1]
    b - a # = [object2]

    # WHY ARE THESE NOT EMPTY?!?!