# Creating a \<=\> method

**URL:** <https://www.ruby-forum.com/t/creating-a-method/50001>\
**Category:** Ruby\
**Created:** [December 11, 2005, 10:30pm UTC](https://www.ruby-forum.com/t/creating-a-method/50001 "2005-12-11T22:30:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dirk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@dirk](https://www.ruby-forum.com/u/dirk)\
**Post date:** [December 11, 2005, 10:30pm UTC](https://www.ruby-forum.com/t/creating-a-method/50001/1 "2005-12-11T22:30:16Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dirk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@dirk](https://www.ruby-forum.com/u/dirk)\
**Post date:** [December 11, 2005, 10:36pm UTC](https://www.ruby-forum.com/t/creating-a-method/50001/2 "2005-12-11T22:36:17Z")

</div>

On Dec 11, 2005, at 3:29 PM, Dirk M. wrote:

> 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 G. II

---

<div class="post-metadata">

**Author:** ![dirk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@dirk](https://www.ruby-forum.com/u/dirk)\
**Post date:** [December 11, 2005, 10:42pm UTC](https://www.ruby-forum.com/t/creating-a-method/50001/3 "2005-12-11T22:42:19Z")

</div>

2005/12/11, James Edward G. II [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Like this?
> 
> def \<=\>( other )  
> sym.to\_s \<=\> other.sym.to\_s  
> end
> 
> James Edward G. II

it works, thanks! 😃  
didn’t think it would be that simple…  
greetings, Dirk.

---

<div class="post-metadata">

**Author:** ![dirk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@dirk](https://www.ruby-forum.com/u/dirk)\
**Post date:** [December 11, 2005, 10:43pm UTC](https://www.ruby-forum.com/t/creating-a-method/50001/4 "2005-12-11T22:43:46Z")

</div>

Dirk M. wrote:

> 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

---

<div class="post-metadata">

**Author:** ![dirk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@dirk](https://www.ruby-forum.com/u/dirk)\
**Post date:** [December 11, 2005, 10:39pm UTC](https://www.ruby-forum.com/t/creating-a-method/50001/5 "2005-12-11T22:39:19Z")

</div>

Dirk M. 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.  
You can generally realy on some other \<=\>, i.e.

> > Foo= Struct.new :someval do  
> > ?\> def \<=\> other
> 
> > someval.to\_s \<=\> other.someval.to\_s  
> > end  
> > include Comparable  
> > end  
> > =\> Foo
> 
> > f1=Foo.new “ciao”  
> > =\> #
> 
> > f2=Foo.new “miao”  
> > =\> #
> 
> > f1 \< f2  
> > =\> true
> 
> > f1 \>= f2  
> > =\> false

hope this helps
