DEV Community

Cover image for Ruby Spaceship
rockshellS
rockshellS

Posted on

Ruby Spaceship

Hey everyone. Welcome to my first ever blog post. The past 20 years I have been working in the music industry putting together live events as a production manager. I'm not going to lie, it's been a wild and crazy ride. Covid-19 hit my career pretty hard, with no comeback in sight, so I've decided to challenge myself and learn some software engineering skills. Here at FlatIron, information is dealt out on such a fast pace, yet I can tell I'm learning a few things! Definitely challenging, but so damn exciting when I can get something to work!
Before I joined my cohort I was watching a ton of YouTube videos about Ruby. This is when the spaceship operator, (combined comparison operator), showed up. I realized after watching him that all the information I was taking in, particularly Ruby, was starting to register and make sense! LOL, the word "spaceship". see this symbol <=> looks similar to
this....download

So, let's see the tech stuff.

CpwOf

the spaceship operator compares two objects:

1 <=> 1 #=> 0 because the two objects are equal
1 <=> 2 #=> -1 because the second object's value is larger
2 <=> 1 #=> 1 because the first object's value is larger
2 <=> "nope" #=> nil if either object is not comparable then the spaceship operator

another example:

100 <=> 100 #=> 0
-55 <=> 256 #=> -1
999 <=> 23 #=> 1
-100 <=> "not again" #=> nil

I watched a great example by Jesus Castello on how to use this operator. I'll show you his code...

numbers = array (1..10)
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

target = 5
=> 5

Jesus suggests we could find all the numbers less than the target and find all the numbers greater than the target by writing this code.

less_than =[]
greater_than = []

numbers.each do |n|
if n < target
less_than << n
end

if n > target
greater_than << n
end
end

less_than. => [1, 2, 3, 4]
greater_than. => [6, 7, 8, 9, 10]

If we use the spaceship operator we can get all 3 values! Less_than, greater_than and equal. To do this we will use group_by method.

numbers = Array(1..10)
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

target = 5
=> 5

numbers.group_by{|n| n <=> target}
=> {-1=>[1, 2, 3, 4], 0=>[5], 1=>[6, 7, 8, 9, 10]}

This will help us when wanting to sort array's. I came across a jakedaywilliams youtube and he demonstrated this way of sorting an array. I'll use my covid pod names as an example

pod = ["Rachelle", "Alison", "Jason", "Britt", "Joshua", "Amanda"]

If I wanted to sort them in ASEC order I could
p pod.sort! {|a,b| a <=> b }

["Alison", "Amanda", "Britt", "Jason", "Joshua", "Rachelle"]
=> ["Alison", "Amanda", "Britt", "Jason", "Joshua", "Rachelle"]

how about DESC order
p pod.sort! { |a,b| b <=> a }

["Rachelle", "Joshua", "Jason", "Britt", "Amanda", "Alison"]
=> ["Rachelle", "Joshua", "Jason", "Britt", "Amanda", "Alison"]

Pretty neat, right? Can we also have a target name to work with? Let's try... we'll use Jason as a target:

pod = ["Rachelle", "Alison", "Jason", "Britt", "Joshua", "Amanda"]
=> ["Rachelle", "Alison", "Jason", "Britt", "Joshua", "Amanda"]

target = "Jason"

pod.group_by {|name| name <=> target}
=> {1=>["Rachelle", "Joshua"], -1=>["Alison", "Britt", "Amanda"], 0=>["Jason"]}

  • note - this does return our array into a hash that has 3 key-value pairs.
  • 1 =>["Rachelle", "Joshua"]
  • -1 =>["Alison", "Britt", "Amanda"]
  • 0 =>["Jason"]

I hope you found this useful. Cheers to persistence and the continuity of learning!

Top comments (0)