- 使用
else if
比elsif
安全吗? - 使用
elsif
是否更好,因为它遵循Ruby的输入 - 或者这是一种偏好?
这是一段取自一本书的代码.我额外添加了end
个关键词,并用else if
个关键词替换了elsif
个关键词.
def describe(inhabitant)
if inhabitant == "sophie"
puts 'gender: female'
puts 'height: 145'
else if inhabitant == "paul"
puts 'gender: male'
puts 'height: 145'
else if inhabitant == "dawn"
puts 'gender: female'
puts 'height: 170'
else if inhabitant == "brian"
puts 'gender: male'
puts 'height: 180'
else if
puts 'species: Trachemys scripta elegans'
puts 'height: 6'
end
end
end
end
end
end
这让我意识到else if
是多么丑陋.