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 if statement before: if condition # Do something! end If the “do something” is a short, simple expression, however, we can move it up into a single line (as you saw in the l…
  3. 3
    You can do the exact same thing with the unless statement. The order is the same as before: something for Ruby to do, the unless keyword, and then an expression that evaluates to true or false. Re…
  4. 4
    During your Ruby adventures, you’ve seen that you often have many options when it comes to accomplishing any one goal. The if statement is no exception! An even more concise version of if/else is …
  5. 5
    The if/else statement is powerful, but we can get bogged down in ifs and elsifs if we have a lot of conditions to check. Thankfully, Ruby provides us with a concise alternative: the case statement….
  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 conditional a…
  7. 7
    Now it’s your turn!
  8. 8
    We know that methods in Ruby can return values, and we ask a method to return a value when we want to use it in another part of our program. What if we don’t put a return statement in our method de…
  9. 9
    Recall that we have the boolean operators and (&&) and or (||) in Ruby. The && operator only returns true when the expressions on both sides of the operator are true; || returns true when…
  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 for loop, and while Ruby does include for loops, there ar…
  11. 11
    If we know the range of numbers we’d like to include, we can use .upto and .downto. This is a much more Rubyist solution than trying to use a for loop that stops when a counter variable hits a cert…
  12. 12
    Remember when we mentioned that symbols are awesome for referencing method names? Wel…
  13. 13
    Speaking of pushing to arrays, Ruby has some nice shortcuts for common method names. As luck would have it, one is for .push! Instead of typing out the .push method name, you can simply use [1, …
  14. 14
    You can always use plain old + or I love espresso “I love “ I love espresso But if you want to do it for non-string values, you have to use .to_s to make it a string: age = 26 “I am “ + age….
  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. three = 3 puts three == 3 ? “Of course.” : “What?” # ==> puts “Of course.” The example above is just a syntax reminder.
  17. 17
    Excellent. Regular if/else statements aren’t the only ones we can refactor, though—a chain of if/elsif/else 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 returns from our code.
  20. 20
    All right! Last one: let’s do something about the decidedly un-Ruby for loop in the editor. 10.times do puts “Knock knock.” puts “Who’s there?” end
  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 return statement in this code that we can change from explicit to implicit! Recall that Ruby will automat…
  4. 4
    Fantastic! You really improved that code, and all it took was a little Ruby know-how.