Ruby best practices Flashcards
1
Q
simplify this if statement:
if condition
#do_something
en
A
do_something if condition
2
Q
write this if in the ternary way: if 5>3 puts "This is true" else puts "Nope!" end
A
puts 5>3 ? “This is true” : “Nope!”
3
Q
how do you refactor a log elsif statement with a lot of conditions:
A
with the case condition statement, here is an example:
case greeting when "English" puts "Hello!" when "French" puts "Bonjour!" when "German" puts "Guten Tag!" when "Finnish" puts "Haloo!" else puts "I don't know that language!" end
4
Q
assign the value 5 to the variable my_num if there is no other value already assigned
A
my_num ||= 5
5
Q
which value does a ruby function return if we did not explicitly mentioned the return?
A
it will return the last evaluated expression.
6
Q
how does respond_to work?
A
age = 26
age.respond_to?(:next) #only takes a symbol
7
Q
how does the shovel works?
A
my_array = [1, 2, 3, 4]
my_array