Key Concepts

Review core concepts you need to learn to master this subject

Ruby Combined Comparison Operator

puts "Keanu" <=> "Adrianna" # The first letters of each word are compared in ASCII order and since "K" comes after "A", 1 is printed. puts 1 <=> 2 # -1 puts 3 <=> 3 # 0 #<=> can also be used inside of a block and to sort values in descending order: my_array = [3, 0, 8, 7, 1, 6, 5, 9, 4] my_array.sort! { |first_num, second_num| second_num <=> first_num } print my_array #Output => [9, 8, 7, 6, 5, 4, 3, 1, 0]

In Ruby, the combined comparison operator, <=>, also known as the spaceship operator is used to compare two objects. It returns 0 if the first operand equals the second, 1 if the first operand is greater than the second, and -1 if the first operand is less than the second.

Methods, Blocks, & Sorting
Lesson 1 of 2
  1. 1
    A method is a reusable section of code written to perform a specific task in a program. You might be wondering why you need to separate your code into methods, rather than just writing everythi…
  2. 2
    Methods are defined using the keyword def (short for “define”). Methods have three parts: 1. The header, which includes the def keyword, the name of the method, and any arguments the method ta…
  3. 3
    Now it’s time for you to build your own method. Remember, the syntax looks like this: def method_name # Do something! end
  4. 4
    Defining a method is great, but it’s not much use to you unless you call it, or tell your program to execute it. For example, if you call a method called cartoon_fox, the program will start looki…
  5. 5
    If a method takes arguments, we say it accepts or expects those arguments. We might define a function, square, like so: def square(n) puts n ** 2 end and call it like this: square(12) # ==>…
  6. 6
    Speaking of not knowing what to expect: your methods not only don’t know what arguments they’re going to get ahead of time but occasionally, they don’t even know how many arguments there will be. …
  7. 7
    Sometimes we don’t just want a method to print something to the console, but we actually want that method to hand us (or another method!) back a value. For that, we use return. def double(n) ret…
  8. 8
    You won’t become a Master Method Maker ‘til you make a mess of methods. (Say that three times fast.) def by_five?(n) return n % 5 == 0 end The example above is just a reminder on how to defi…
  9. 9
    Most methods that you’ve worked with have defined names that either you or someone else gave them (i.e. [array].sort(), “string”.downcase(), and so on). You can think of blocks as a way of creati…
  10. 10
    There are some differences between blocks and methods, however. Check out the code in the editor. The capitalize method capitalizes a word, and we can continually invoke the capitalize method by n…
  11. 11
    A method can take a block as a parameter. That’s what .each has been doing this whole time: taking a block as a parameter and doing stuff with it! You just didn’t notice because we didn’t use the o…
  12. 12
    Sorting arrays is a very common problem in computer science, and is well studied for that reason. There are many algorithms —well-defined sequences of steps—each with its own trade-offs and advan…
  13. 13
    If we were to hand you five books and ask you to arrange them, sorted by title, on a shelf, how would you do it? Most sorting algorithms assume we are sorting an array of items, which involves com…
  14. 14
    We can also use a new operator called the combined comparison operator to compare two Ruby objects. The combined comparison operator looks like this: . It returns 0 if the first operand (item …
  15. 15
    What if we wanted to sort the books by title, but from Z – A, or descending order? It appears that Ruby’s sort method only works for A – Z, or ascending order. The sort method assumes by default …
  16. 16
    Let’s quickly review how to create a basic Ruby method. def double(n) return n * 2 end The example above is just a syntax reminder.
  17. 17
    Good! Now let’s make our method a bit more complex by adding arguments and a return statement. def double(n) return n * 2 end
  18. 18
    Let’s go over what we learned about blocks. numbers = [5, 2, 8] sum = 0 numbers.each do |n| sum += n end puts sum The example above is just a reminder about syntax. We calculate the sum of a l…
  19. 19
    Finally, let’s review what we learned about sorting. books.sort! do |firstBook, secondBook| firstBook secondBook end Remember that the above example was how we sorted in alphabetical order.