[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

check if 2 objects are equal

junkone

8/1/2006 2:11:00 AM

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,:otherInfo;
def to_s
return @symbol + @news + @exchange + @name
end


end

4 Answers

S Wayne

8/1/2006 3:20:00 AM

0

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

Tim Hunter

8/1/2006 12:15:00 PM

0

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

?cg?

8/31/2010 1:08:00 PM

0

On Tue, 31 Aug 2010 05:21:08 -0700, Mark <sac.easy.riders@gmail.com>
wrote:

>On 2010-08-30 17:57:59 -0700, °cg° said:
>
>> On Mon, 30 Aug 2010 20:52:17 -0400, "JayMac" <macjay@optonline.net>
>> wrote:
>>
>>> http://www.merck.com/mmhe/resources/pronunciations/in...
>>>
>>> For those of you who wondered how to pronounce some medical words.
>>
>> Nifty find. Thanks. I'm going to resolve a bet with this so, double
>> thanks. The buck I win will pay for my next pot of coffee with change
>> coming back.
>
>What word were you not able to look up prior to this posting? I'm
>curious only because i find it difficult to believe that a googling
>professional such as yourself should have been able to resolve your
>little issue before now.

Some things are worth the effort and some are not.

--
°cg°

Fortune Cookie: A conclusion is simply where you
arrive when you get tired of thinking.

Mark

8/31/2010 1:54:00 PM

0

On 2010-08-31 06:08:12 -0700, °cg° said:

> On Tue, 31 Aug 2010 05:21:08 -0700, Mark <sac.easy.riders@gmail.com>
> wrote:
>
>> On 2010-08-30 17:57:59 -0700, °cg° said:
>>
>>> On Mon, 30 Aug 2010 20:52:17 -0400, "JayMac" <macjay@optonline.net>
>>> wrote:
>>>
>>>> http://www.merck.com/mmhe/resources/pronunciations/in...
>>>>
>>>> For those of you who wondered how to pronounce some medical words.
>>>
>>> Nifty find. Thanks. I'm going to resolve a bet with this so, double
>>> thanks. The buck I win will pay for my next pot of coffee with change
>>> coming back.
>>
>> What word were you not able to look up prior to this posting? I'm
>> curious only because i find it difficult to believe that a googling
>> professional such as yourself should have been able to resolve your
>> little issue before now.
>
> Some things are worth the effort and some are not.

Interesting comment for a man with nothing but time on his hands not to
mention the opportunity to gain financially for his efforts. Your
day's must be full.

--
Mark
Sacramento Easy Riders
Never ride faster than your guardian angel can fly. ~Author Unknown

"imagine me with my hands held over my head without a clue"
CG, Liar & Coward