You have traveled far young one. You are almost ready for your second challenge, but you will need a more powerful weapon in the fight against something or other...(can't always be clever, can we?)
One of the most venerated virtues in Ruby is to be DRY. DRY stands for "Don't Repeat Yourself" and it is a practice that can radically streamline your programs. Of course the best way to achieve DRYness is to create as many reusable pieces of code as possible. Enter methods. Methods are just functions in Ruby. You can repeatedly use the same method without constantly rewriting it. In Ruby, once you have written a method you can even use that method in other programs! Here is a method declaration:
def greeting
puts "Hello world!"
end
We declare methods using the def
(short for "define") keyword. Method declarations are made up of three parts: header, body, and end. The header contains def
, the method name, and any method arguments. The body contains the code block to be executed. The body is conventionally indented two spaces (remember, "Convention Over Configuration!"). The method then ends with end
. Check out another method declaration:
def cube(n)
puts n ** 3
end
puts cube(7)
Here we define the method cube
that takes one number as argument n
. The code block raises to the third power whatever number you call the method on. In the example we call the method on the number 7
which returns 343
. More on methods soon.
Most methods that we have seen have defined names. Blocks are a way of creating methods that don't need names. Blocks in Ruby are demarcated by either the do
/end
or {}
syntax:
[true, false, true, true, false].each {|bool| puts bool}
We have seen code blocks before in for
and while
loops as well as in control flow statements. Methods can take blocks as arguments, as is the case of the .each
method. It takes the code block and applies it to an array. Very cool. Passing a block to a method helps abstract tasks from the method. Very DRY.
One of the most important things we need to be able to do with a language is sort data. Ruby has two built-in sorting methods .sort
and .sort!
. When used with the combined comparison operator <=>
we can reorder arrays pretty easily. Here's the syntax:
drinks = ['red bull', 'monster', 'full-throttle', 'amp', 'rockstar']
drinks.sort! {|drink1, drink2| drink2 <=> drink1}
puts drinks
The .sort!
takes the block with our <=>
operator as argument and runs it over the drinks
array. In our example, the <=>
compares the two operands drink1
and drink2
and returns 0
if drink1 == drink2
, 1
if drink1 > drink2
, and -1
if drink1 < drink2
.
The block passed into .sort!
must return 1
, 0
,, or -1
, hence our use of the <=
. Intuitively if it returns -1
, drink1
comes before drink2
. If it returns 0
the two elements are of equal weight and order is preserved. If it returns 1
the reverse is true and drink2
comes before drink1
. The result of the above code is:
amp
full-throttle
monster
red bull
rockstar
Sorting is just one of the great things you can do with methods follow along in the video below as I guide you through examples from this lesson as well as another mini project!