I am practicing the following problem in Ruby. https://leetcode.com/problems/defanging-an-ip-address/
I find that I need to have a parentheses in my ternary operator to get the same result as the equivalent line of code with the if/else condition. Why is that ?
# @param {String} address
# @return {String}
def defang_i_paddr(address)
return address if address.isEmpty?
result_from_ternary = ''
result_from_if = ''
address.each_char do |letter|
# Why do I need parentheses here ?
result_from_ternary << (letter == '.' ? '[.]' : letter)
# Why dont I need parentheses here ?
result_from_if << if letter == '.'
'[.]'
else
letter
end
end
result_from_ternary = ''
end