Ruby best practices Flashcards

1
Q

simplify this if statement:
if condition
#do_something
en

A

do_something if condition

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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!”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

assign the value 5 to the variable my_num if there is no other value already assigned

A

my_num ||= 5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

how does respond_to work?

A

age = 26

age.respond_to?(:next)
#only takes a symbol
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

how does the shovel works?

A

my_array = [1, 2, 3, 4]

my_array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly