Ruby conventions Flashcards
How do you write a single line if statement?
puts “It’s true!” if true
How do you write a single line unless statement?
puts ‘Hello’ unless false
What is a ternary conditional expression?
A if/else statement is a ternary expression beacuse it takes three arguments:
- A boolean
- a expression to evaluate if the boolean is true
- a expression to evaluate if the boolean is false
boolean ? Do this if true: Do this if false
What is a case statement?
A case statement is a if/else statement that is used when there are a lot of conditions to check.
case language when "JS" then put "Websites!" when "Python" then puts "Science!" when "Ruby" then puts "Web apps!" else puts "I don't know!" end
What is a conditional assignment?
A conditional assignment assigns a variable to a name if the name is nil
favorite_animal = ‘cat’
favorite_animal | |= ‘dog’
print favorite_animal => cat
favorite_animal = nil
favorite_animal | |= ‘dog’
print favorite_animal => ‘dog’
What is a implicit return?
It means that ruby will return a value even though you did not require it.
What is a short-circuit evaluation?
It means in a conditional statement with two conditions the second condition is evaluated only when the first condition is not enough to determine the value of expression
false && true
What is the concatenation operator?
It is «
and used to push in a element to the end of an array or a string.
[1, 2, 3] «_space;4 => [1, 2, 3, 4]
‘Faraz ‘ «_space;‘Naeem’ => ‘Faraz Naeem’
What is string interpolation?
{ x } its when you add a value to a string, no need to convert it.
What is refactoring?
Refactoring means that one should make the code simpler and more readable whitout changing its properties or function