ruby - Checking for nil string before concatenating -
This question is very similar to many queries, but it is not that it is anything from a duplicate. Is about and writing better code for zero / zero probes.
Currently I have:
file.puts "cn:" + (Var1.nil ?? "UNKNOWN": var1)
Which works fine but does not look good What is a better way to write it in Ruby that I am checking for zero and not including it
You can:
file.puts" cn: "+ (var1 ||" UNKNOWN ")
< / Pre>Or, if you prefer:
file.puts "cn:" + (var1 or "UNKNOWN")
Or my favorite, which I think is The most idiomatic Ruby is:
file.puts "cn: # {var1 or 'unknown'}"
Comments
Post a Comment