What is the difference bw puts("Odin") and puts "Odin"?
Context
puts "odin" || true gives different result than puts("odin") || true
Difference bw puts with braces() and without braces
61 views Asked by koil At
3
There are 3 answers
0
On
braces are optional in ruby, or better said: in some cases spaces act as like braces would.
the reason you get two different results is because of where the braces are interpreted, so both the following statements print "odin" and return nil:
puts "odin" || true
puts("odin" || true)
Remember here that strings are truthy in ruby, meaning we actually have a condition inside the puts method, ie true || false #=> true
As for the second example, the condition is no longer passed as a argument to the puts method. puts returns nil, which is falsey.
puts("odin") || true #=> true
this means the overall return is nil || true #=> true
Ruby understands
puts "odin" || trueasputs("odin" || true)which will always outputodin. It will not evaluate the|| truepart because"odin"is already truthy. And that line will returnnilbecause that is the default return value of theputsmethod.puts("odin") || truewill outputodintoo but will returntruebecause the return value ofputs("odin")isniland therefore the|| truewill be evaluated and thetruewill be returned.