Blocks and Sorting
Learn how to define your own methods, as well as how to use blocks to develop powerful sorting algorithms.
StartKey Concepts
Review core concepts you need to learn to master this subject
Ruby Combined Comparison Operator
Ruby Method Splat
Ruby Block Parameter
Ruby Return
Ruby Sort Method
Ruby Method Parameters & Arguments
Ruby method
Ruby Block
Ruby Combined Comparison Operator
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
- 1A 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…
- 2Methods 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…
- 3Now it’s time for you to build your own method. Remember, the syntax looks like this: def method_name # Do something! end
- 5If 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) # ==>…
- 7Sometimes 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…
- 8You 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…
- 9Most 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…
- 10There 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…
- 11A 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…
- 12Sorting 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…
- 13If 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…
- 14We 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 …
- 15What 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 …
- 16Let’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.
- 17Good! Now let’s make our method a bit more complex by adding arguments and a return statement. def double(n) return n * 2 end