check if 2 objects are equal

Discussion in 'Ruby' started by junkone@rogers.com, Aug 1, 2006.

  1. Guest

    Hi
    I havea a class with this structure. how do i check if 2 objects
    instanialted from this class are equal. they could be separate
    instantialtion but have exactly the same values?


    class SymbolObject

    def initialize(symb)
    puts symb
    @symbol=symb

    @name=@exchange=@news=@summary=@sector=@industry=@category=@otherInfo=@scanType=""
    @markedBad=false
    end
    attr_accessor :symbol,:name ,:exchange, :news, :summary, :sector,
    :industry,:category,:markedBad,:scanType,:eek:therInfo;
    def to_s
    return @symbol + @news + @exchange + @name
    end


    end
     
    , Aug 1, 2006
    #1
    1. Advertising

  2. N Okia Guest

    If you want to define equivalence for your classes, you really need to
    do so yourself.

    The easiest way is to make the class Comparable, and define your own
    <=> operator. You can be as simple or as complex as you like:

    class MyClass
    include Comparable

    attr_accessor :my_age, :my_first_name, :my_last_name

    def initialize(age,first_name,last_name)
    @my_age = age.to_i
    @my_first_name = first_name
    @my_last_name = last_name
    end

    def <=>(other)
    # if the other class being compared is nil, we aren't equivalent
    return nil if other.nil?

    # Simple case, if both objects have same objectid, they are the same
    return 0 if object_id == other.object_id

    # Define a heirarchy of tests for this class
    if (@my_age == other.my_age)
    if (@my_first_name <=> other.my_first_name) == 0
    return @my_last_name <=> other.my_last_name
    else
    return @my_first_name <=> other.my_first_name
    end
    else
    return @my_age <=> other.my_age
    end
    end

    end
     
    N Okia, Aug 1, 2006
    #2
    1. Advertising

  3. Tim Hunter Guest

    N Okia wrote:
    > If you want to define equivalence for your classes, you really need to
    > do so yourself.
    >
    > The easiest way is to make the class Comparable, and define your own
    > <=> operator. You can be as simple or as complex as you like:
    >
    > class MyClass
    > include Comparable
    >
    > attr_accessor :my_age, :my_first_name, :my_last_name
    >
    > def initialize(age,first_name,last_name)
    > @my_age = age.to_i
    > @my_first_name = first_name
    > @my_last_name = last_name
    > end
    >
    > def <=>(other)
    > # if the other class being compared is nil, we aren't equivalent
    > return nil if other.nil?
    >
    > # Simple case, if both objects have same objectid, they are the same
    > return 0 if object_id == other.object_id
    >
    > # Define a heirarchy of tests for this class
    > if (@my_age == other.my_age)
    > if (@my_first_name <=> other.my_first_name) == 0
    > return @my_last_name <=> other.my_last_name
    > else
    > return @my_first_name <=> other.my_first_name
    > end
    > else
    > return @my_age <=> other.my_age
    > end
    > end
    >
    > end
    >


    The simplest way to do this is:

    def <=>(other)
    return [@my_age, @my_first_name, @my_last_name] <=> [other.age,
    other.my_first_name, other.my_last_name]
    end
     
    Tim Hunter, Aug 1, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Yves Glodt

    test whether 2 objects are equal

    Yves Glodt, Jan 31, 2006, in forum: Python
    Replies:
    5
    Views:
    321
    Bruno Desthuilliers
    Feb 1, 2006
  2. 7stud
    Replies:
    11
    Views:
    710
    Dennis Lee Bieber
    Mar 20, 2007
  3. Giff
    Replies:
    4
    Views:
    515
    Lionel B
    May 15, 2007
  4. Slaunger
    Replies:
    11
    Views:
    414
    Steven D'Aprano
    Aug 8, 2008
  5. Frederik
    Replies:
    5
    Views:
    987
    lewbloch
    Jul 20, 2011
Loading...

Share This Page