creating a <=> method

D

Dirk Meijer

------=_Part_23192_16506269.1134336590849
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

hi everyone,
i've created a class, and want it to be comparable with itself, but i don't
know what to place inside the <=3D> method.
the basic idea is that one of the values of the class (which is a symbol) i=
s
converted to a string, which is then compared.
greetings, Dirk.

------=_Part_23192_16506269.1134336590849--
 
J

James Edward Gray II

hi everyone,
i've created a class, and want it to be comparable with itself, but
i don't
know what to place inside the <=> method.
the basic idea is that one of the values of the class (which is a
symbol) is
converted to a string, which is then compared.

Like this?

def <=>( other )
sym.to_s <=> other.sym.to_s
end

James Edward Gray II
 
G

gabriele renzi

Dirk Meijer ha scritto:
hi everyone,
i've created a class, and want it to be comparable with itself, but i don't
know what to place inside the <=> method.
the basic idea is that one of the values of the class (which is a symbol) is
converted to a string, which is then compared.
greetings, Dirk.

yuu should return 1 if self > b, 0 if self == b and -1 otherwise.
=> false

hope this helps
 
D

Dirk Meijer

------=_Part_23256_10842118.1134337215610
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

2005/12/11 said:
Like this?

def <=3D>( other )
sym.to_s <=3D> other.sym.to_s
end

James Edward Gray II


it works, thanks! :D
didn't think it would be that simple..
greetings, Dirk.

------=_Part_23256_10842118.1134337215610--
 
E

Eero Saynatkari

Dirk said:
hi everyone,
i've created a class, and want it to be comparable with itself, but i
don't
know what to place inside the <=> method.
the basic idea is that one of the values of the class (which is a
symbol) is
converted to a string, which is then compared.

# The spaceship operator needs to return either
# -1, 0 or 1 depending on whether it is 'smaller',
# 'equal' or 'greater' than the other object.
def <=>(other)
# We can use String#<=> here if both are Strings.
# Otherwise implement some custom comparison as above.
@my_value.to_s <=> other.to_s
end

greetings, Dirk.

E
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top