# == operator - How does it work?

**URL:** <https://www.ruby-forum.com/t/operator-how-does-it-work/85328>\
**Category:** Ruby\
**Created:** [February 12, 2007, 6:09pm UTC](https://www.ruby-forum.com/t/operator-how-does-it-work/85328 "2007-02-12T18:09:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bruparel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e47774/32.png) [@bruparel](https://www.ruby-forum.com/u/bruparel)\
**Post date:** [February 12, 2007, 6:09pm UTC](https://www.ruby-forum.com/t/operator-how-does-it-work/85328/1 "2007-02-12T18:09:01Z")

</div>

I am running the following code from Programming Ruby book on page 120:

class Song  
include Comparable  
def initialize(name, artist, duration)  
@name = name  
@artist = artist  
@duration = duration  
@plays = 0  
end  
def \<=\>(other)  
self.duration \<=\> other.duration  
end  
end

song1 = Song.new(“My Way”, “Sinatra”, 225)  
song2 = Song.new(“Bicylops”, “Fleck”, 260)

puts song1 == song1

It is printing true for the puts song1 == song2 statement. How are  
these two song objects equal?

Thanks in advance for your time.

Bharat

---

<div class="post-metadata">

**Author:** ![bruparel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e47774/32.png) [@bruparel](https://www.ruby-forum.com/u/bruparel)\
**Post date:** [February 12, 2007, 6:25pm UTC](https://www.ruby-forum.com/t/operator-how-does-it-work/85328/2 "2007-02-12T18:25:36Z")

</div>

On Feb 12, 12:09 pm, Bharat R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am running the following code from Programming Ruby book on page 120:  
> …  
> puts song1 == song1
> 
> It is printing true for the puts song1 == song2 statement. How are  
> these two song objects equal?

Note you have ‘song1 == song1’ which is of course true.

Based on the implementation of the spaceship op (\<=\>) and the  
inclusion of Comparable,  
song2 has a longer duration so is “greater than” song1

HTH  
Chris

---

<div class="post-metadata">

**Author:** ![bruparel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e47774/32.png) [@bruparel](https://www.ruby-forum.com/u/bruparel)\
**Post date:** [February 12, 2007, 6:40pm UTC](https://www.ruby-forum.com/t/operator-how-does-it-work/85328/3 "2007-02-12T18:40:40Z")

</div>

Get your own thread! 9^)

Message thread and Ruby thread…punny, yes? 8\*)

---

<div class="post-metadata">

**Author:** ![bruparel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e47774/32.png) [@bruparel](https://www.ruby-forum.com/u/bruparel)\
**Post date:** [February 12, 2007, 6:55pm UTC](https://www.ruby-forum.com/t/operator-how-does-it-work/85328/4 "2007-02-12T18:55:48Z")

</div>

Sorry for the bother. I thought I was typing in song1 == song2 and  
printing. Whereas I really typed in song1 == song1.  
One of those days…  
Bharat

---

<div class="post-metadata">

**Author:** ![bruparel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e47774/32.png) [@bruparel](https://www.ruby-forum.com/u/bruparel)\
**Post date:** [February 12, 2007, 6:32pm UTC](https://www.ruby-forum.com/t/operator-how-does-it-work/85328/5 "2007-02-12T18:32:37Z")

</div>

I have a script that goes thru each server in the environment and  
updates a database with the current rpm package info. It’s used for  
tracking, to make sure all servers have the same versions etc. and query  
when something has changed.

It’s been successful for about 1 year now, but as the environment has  
grown it takes longer to complete so I am trying to use ruby threads to  
fire off multiple server checks at the same time.

I added what I believe is the correct logic below, but this does not  
seem to work. All servers get their own thread, and start running. But  
as soon as one completes the entire script exists as if it has completed  
successfully.

Any insight would be appreciated.

Thanks  
Zach

#!/usr/local/bin/ruby  
require ‘thread’  
require “servers”  
require ‘net/ssh’  
require ‘rpmlist’  
require ‘rpmmysql’  
require ‘rpm’

class RPMAudit  
serversObj = Servers.new()  
threads = []  
serversObj.all.each { |x|  
threads \<\< Thread.new(x) { |y|  
hostName = y[1] + “.” + y[2]  
print "HOSTNAME : “, hostName, "  
started:\n”  
result = RPMList.new(hostName)  
if(result.rpmArray == nil) then  
next  
end  
rpmThreads = []  
result.rpmArray.each { |y|  
rpmThreads \<\< Thread.new(y) {  
|z|  
package = RPM.new( z )  
if(package.signature ==  
nil) then next end  
print “.”  
STDOUT.flush  
sql =  
RPMMysql.new(package,hostName)  
}  
rpmThreads.each { |bthread|  
bthread.join }  
}  
print "HOSTNAME : “, hostName, "  
ended:\n”  
}  
}  
threads.each { |aThread| aThread.join }  
end
