== operator - How does it work?

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

On Feb 12, 12:09 pm, Bharat R. [email protected] 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

Get your own thread! 9^)

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

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

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