Refactoring

TBD

Start[missing "en.views.course_landing_page.learn-ruby.course_illustration" translation]
Chevron Left Icon
The Zen of Ruby
Lesson 1 of 2
Chevron Right Icon
  1. 1

    As a language, Ruby prioritizes programmer productivity over program optimization. This means that Ruby may not always run a program in the fastest way possible, but it strives to be a language tha...

  2. 2

    You've seen the Ruby [...] statement before: [...] If the "do something" is a short, simple expression, however, we can move it up into a single line (as you saw in the last exercise). The synt...

  3. 3

    You can do the exact same thing with the [...] statement. The order is the same as before: something for Ruby to do, the [...] keyword, and then an expression that evaluates to [...] or [......

  4. 4

    During your Ruby adventures, you've seen that you often have many options when it comes to accomplishing any one goal. The [...] statement is no exception! An even more concise version of [...]...

  5. 5

    The [...] / [...] statement is powerful, but we can get bogged down in [...] s and [...] s if we have a lot of conditions to check. Thankfully, Ruby provides us with a concise alternative: the ...

  6. 6

    We've seen that we can use the [...] operator to assign a value to a variable. But what if we only want to assign a variable if it hasn't already been assigned? For this, we can use the conditi...

  7. 7

    Now it's your turn!

  8. 8

    We know that methods in Ruby can return values, and we ask a method to [...] a value when we want to use it in another part of our program. What if we don't put a [...] statement in our method ...

  9. 9

    Recall that we have the boolean operators and ( [...] ) and or ( [...] ) in Ruby. The [...] operator only returns [...] when the expressions on both sides of the operator are true; [...

  10. 10

    Sooner or later, you're going to need to perform a repetitive task in your programs. Many programming languages allow you to do this with a [...] loop, and while Ruby does include [...] loops, ...

  11. 11

    If we know the range of numbers we'd like to include, we can use [...] and [...] . This is a much more Rubyist solution than trying to use a [...] loop that stops when a counter variable hits ...

  12. 12

    Remember when we mentioned that symbols are awesome for referencing method names? Well, [...] takes a symbol and returns [...] if an object can receive that method and [...] otherwise. For ...

  13. 13

    Speaking of pushing to arrays, Ruby has some nice shortcuts for common method names. As luck would have it, one is for [...] ! Instead of typing out the [...] method name, you can simply use [...

  14. 14

    You can always use plain old [...] or [...] to add a variable value into a string: [...] But if you want to do it for non-string values, you have to use [...] to make it a string: [...]...

  15. 15

    All right! Time to put your new knowledge to work by refactoring some existing code. Refactoring is just a fancy way of saying we're improving the structure or appearance of our code without ch...

  16. 16

    Good! Let's make our code even more streamlined using the ternary operator. [...] The example above is just a syntax reminder.

  17. 17

    Excellent. Regular [...] / [...] statements aren't the only ones we can refactor, though—a chain of [...] / [...] / [...] statements can clean up really nicely, too!

  18. 18

    Perfect! Now let's review conditional assignment. We'll take a break from strict editing mode and let you do a bit more writing.

  19. 19

    Next up: let's simplify our method madness by removing unnecessary [...] s from our code.

  20. 20

    All right! Last one: let's do something about the decidedly un-Ruby [...] loop in the editor. [...]

  1. 1

    We're going to reinforce our knowledge of Ruby best practices by refactoring some existing code. As mentioned, refactoring is the process by which we improve a code's structure, appearance, and...

  2. 2

    One of the most common suggestions when it comes to writing is to omit needless words, and it applies just as much to writing Ruby as writing stories. There are two control structures to change he...

  3. 3

    Great work! This code looks better already. We can remove even more, however. There's one [...] statement in this code that we can change from explicit to implicit! Recall that Ruby will automa...

  4. 4

    Fantastic! You really improved that code, and all it took was a little Ruby know-how.

Refactoring

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