TL;DR - Replacing elsif
with else if
is alright for a conditional with only 2 paths. Remember to close the second if
conditional created with else if
. It is best practice to have as few levels of conditionals as you can, making for a less-complex method. So err on the side of caution and use elsif
.
Depending on how you plan to write your method, else if
may work. It is not a good habit to get into, however.
Take the following example. There are only 2 conditions. The second condition looks similar to elsif
, but is interpreted as the second chunk of code:
# What you may want to write
if true
puts 'true'
else if false
puts 'false'
end
# How Ruby requires it
if true
puts 'true'
else
if false # You may also do: puts 'false' if false
puts 'false'
end
end
The first block will search for another end
to close the primary conditional. Take note, you can bypass the extra end with a single line if
statement. (I only suggest this if the executed code can be written on a single line.)
It is important to note that once you declare an else
, you may have no other conditionals in the same tier as that else
. Given the second example above, the second if
is nested under the else
. If you were to call else
or elsif
on the same tier as the initial else
, the conditional will fail.
Here is when you would not want to implement else if
:
def describe(inhabitant)
if inhabitant == "sophie"
puts 'gender: female'
puts 'height: 145'
elsif inhabitant == "paul"
puts 'gender: male'
puts 'height: 145'
elsif inhabitant == "dawn"
puts 'gender: female'
puts 'height: 170'
elsif inhabitant == "brian"
puts 'gender: male'
puts 'height: 180'
else
puts 'species: Trachemys scripta elegans'
puts 'height: 6'
end
end
Notice that neither of the elsif
statements can be "converted" to else if
in a clean way.
UPDATE: Thanks to Stefan, you can still use else if
, leading to a very nested method.
https://gist.github.com/sos4nt/a41b36d21f6eec5e0a42