# Space Ship Operator \<=\> Odd Behaviour

**URL:** <https://www.ruby-forum.com/t/space-ship-operator-odd-behaviour/195685>\
**Category:** Ruby\
**Created:** [September 28, 2010, 4:13am UTC](https://www.ruby-forum.com/t/space-ship-operator-odd-behaviour/195685 "2010-09-28T04:13:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nallan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/0ea827/32.png) [@nallan](https://www.ruby-forum.com/u/nallan)\
**Post date:** [September 28, 2010, 4:13am UTC](https://www.ruby-forum.com/t/space-ship-operator-odd-behaviour/195685/1 "2010-09-28T04:13:48Z")

</div>

I have come across a strange occurance when I use the space ship  
operator to compare percentages.

I want to compare percentages to see if they are greater than, less than  
or equal to 85%, this mostly works fine - all the way up to 99% - it  
returns a 1 obviously meaning that 99% is greater than 85%

however the strangeness comes in when I attempt to compare 100% or  
greater against 85% - it returns -1

I am fairly new to ruby - by no means an expert so I suspect there is  
something wrong with what I am doing rather than the spaceship operator.  
Can someone please shine some light on this for me so that I may better  
understand.

I have the knowledge to perform this operation another way with differnt  
code, but I am interested to know why - if there is a logical  
explaination.

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![nallan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/0ea827/32.png) [@nallan](https://www.ruby-forum.com/u/nallan)\
**Post date:** [September 28, 2010, 4:55am UTC](https://www.ruby-forum.com/t/space-ship-operator-odd-behaviour/195685/2 "2010-09-28T04:55:44Z")

</div>

On Sep 27, 10:13 pm, Nathanial A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I am fairly new to ruby - by no means an expert so I suspect there is  
> Posted viahttp://www.ruby-forum.com/.  
> that is because the sort being performed is alphanumeric instead of  
> numeric. In the example below, z has the correct sorted values. Due to  
> the way to\_i is defined, ‘10%’.to\_i is same as ‘10’.to\_i.

x = [‘35%’, ‘25%’, ‘5%’, ‘10%’, ‘100%’, ‘15%’]  
y = x.sort { |a,b| a \<=\> b }  
z = x.sort { |a,b| a.to\_i \<=\> b.to\_i }  
puts “x is:”  
p x  
puts “y is:”  
p y  
puts “z is:”  
p z

Output:  
x is:  
[“35%”, “25%”, “5%”, “10%”, “100%”, “15%”]  
y is:  
[“10%”, “100%”, “15%”, “25%”, “35%”, “5%”]  
z is:  
[“5%”, “10%”, “15%”, “25%”, “35%”, “100%”]

---

<div class="post-metadata">

**Author:** ![nallan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/0ea827/32.png) [@nallan](https://www.ruby-forum.com/u/nallan)\
**Post date:** [September 28, 2010, 9:09am UTC](https://www.ruby-forum.com/t/space-ship-operator-odd-behaviour/195685/3 "2010-09-28T09:09:41Z")

</div>

On Tuesday 28 September 2010, Nathanial A. wrote:

> |I am fairly new to ruby - by no means an expert so I suspect there is  
> |something wrong with what I am doing rather than the spaceship operator.  
> |Can someone please shine some light on this for me so that I may better  
> |understand.  
> |  
> |I have the knowledge to perform this operation another way with differnt  
> |code, but I am interested to know why - if there is a logical  
> |explaination.  
> |  
> |Thanks in advance!

What kind of object are you using to represent percentages? From the  
way you  
worded your question, I think you’re using strings, that is you’re  
writing  
something like

“99%” \<=\> “85%”

If this is the case, then it’s no wonder that “100%” \<=\> “85%” returns  
-1: the  
spaceship operator on strings compares them in alphabetical order, which  
means  
that “100%” should indeed came first. To compare the two numerical  
values, you  
have to convert them to numerical classes. How to do this depends on  
your  
exact needs. For example, if you’re sure that all your percentages are  
integer  
values, you can simply do something like:

perc\_1=“100%”[0…-1].to\_i  
perc\_2=“85%”[0…-1].to\_i  
perc\_1 \<=\> perc\_2

This should give you the correct result. If your percentages can be non  
integers (i.e. “94.42%”), you’ll have to convert them to Float or  
Rational.

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![nallan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/0ea827/32.png) [@nallan](https://www.ruby-forum.com/u/nallan)\
**Post date:** [September 30, 2010, 8:31am UTC](https://www.ruby-forum.com/t/space-ship-operator-odd-behaviour/195685/4 "2010-09-30T08:31:59Z")

</div>

On 09/28/2010 09:09 AM, Stefano C. wrote:

> > |
> 
> exact needs. For example, if you’re sure that all your percentages are integer  
> values, you can simply do something like:
> 
> perc\_1=“100%”[0…-1].to\_i  
> perc\_2=“85%”[0…-1].to\_i  
> perc\_1\<=\> perc\_2
> 
> This should give you the correct result. If your percentages can be non  
> integers (i.e. “94.42%”), you’ll have to convert them to Float or Rational.

Note: you don’t need the sub range with #to\_i if the number is the  
beginning:

irb(main):001:0\> ‘123%’.to\_i  
=\> 123

Kind regards

robert
