Refactoring
Learn how to refactor code with Ruby’s best practices and conventions in order to vastly improve the readability and structure of a program.
StartKey Concepts
Review core concepts you need to learn to master this subject
Ruby Case Statement
Ruby .respond_to?
Ruby Short-Circuit Evaluation
Ruby Ternary Operator
Ruby .upto and .downto Methods
Ruby Conditional Assignment Operator
Ruby .push Method Alternative
Ruby “if” Statement Short Expression
Ruby Case Statement
Ruby Case Statement
tv_show = "Bob's Burgers"
case tv_show
when "Archer"
puts "I don't like the voice of Archer."
when "Bob's Burgers"
puts "I love the voice of Bob Belcher."
else
puts "I don't know who voices this cartoon."
end
# => I love the voice of Bob Belcher.
#In this example, a case statement is used to check for multiple possible values of tv_show. Since tv_show is "Bob's Burgers", the second when is evaluated to true. If none of the conditions were met, Ruby would evaluate the else statement.
In Ruby, a case
statement is a more concise alternative to an if/else
statement that contains many conditions.
The Zen of Ruby
Lesson 1 of 2
- 1As 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…
- 2You’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…
- 3You 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…
- 4During 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 …
- 5The 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….
- 6We’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…
- 7Now it’s your turn!
- 8We 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…
- 9Recall 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…
- 10Sooner 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…
- 11If 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…
- 12Remember when we mentioned that symbols are awesome for referencing method names? Wel…
- 13Speaking 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, …
- 14You 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….
- 15All 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…
- 16Good! 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.
- 17Excellent. 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!
- 18Perfect! Now let’s review conditional assignment. We’ll take a break from strict editing mode and let you do a bit more writing.
- 19Next up: let’s simplify our method madness by removing unnecessary returns from our code.
- 20All 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