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