Blocks and Sorting

Learn how to define your own methods, as well as how to use blocks to develop powerful sorting algorithms.

Start[missing "en.views.course_landing_page.learn-ruby.course_illustration" translation]
Chevron Left Icon
Methods, Blocks, & Sorting
Lesson 1 of 2
Chevron Right Icon
  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 [...] (short for " [...] ine"). Methods have three parts: 1. The header, which includes the [...] keyword, the name of the method, and any arguments t...

  3. 3

    Now it's time for you to build your own method. Remember, the syntax looks like this: [...]

  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 [...] , the program will start looking f...

  5. 5

    If a method takes arguments, we say it accepts or expects those arguments. We might define a function, [...] , like so: [...] and call it like this: [...] The argument is the piece of c...

  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 [...] . [...] 1. In the...

  8. 8

    You won't become a Master Method Maker 'til you make a mess of methods. (Say that three times fast.) [...] The example above is just a reminder on how to define a method.

  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 [...] method capitalizes a word, and we can continually invoke the [...] method by name. W...

  11. 11

    A method can take a block as a parameter. That's what [...] 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...

  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 [...] if the first _ope...

  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 [...] method assumes by defau...

  16. 16

    Let's quickly review how to create a basic Ruby method. [...] 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 [...] statement. [...]

  18. 18

    Let's go over what we learned about blocks. [...] The example above is just a reminder about syntax. We calculate the sum of a list of numbers.

  19. 19

    Finally, let's review what we learned about sorting. [...] Remember that the above example was how we sorted in alphabetical order.

  1. 1

    We noticed in the last lesson that [...] didn't have a built-in way of handling sorting in reverse alphabetical order. Now that we know how to write our own Ruby methods, we can fix that!

  2. 2

    First, we'll need to define our [...] method.

  3. 3

    Let's start with the new bit of code you saw in exercise 1: [...] The first part makes sense—we're defining a method, [...] . We can guess that the first parameter is an array, but what's this ...

  4. 4

    Great! Now let's add a little logic to our method. [...] 1. In the above example, we create a new array called [...] . 2. Then, we sort the array. 3. Finally, we print out [...] , [...] , [...

  5. 5

    Great! Now we need to add the right logic to our method. [...] 1. In the example above, we create an array called [...] . 2. Then, we reverse the array. Like with [...] , the exclamation mar...

  6. 6

    Well done! You now have your very own method that adds functionality that Ruby didn't have before. Building your own methods is great for using code over and over without having to retype things, ...

Blocks and Sorting

Start[missing "en.views.course_landing_page.learn-ruby.course_illustration" translation]