Checking for nil string before concatenating
Asked Answered
R

6

23

This question is similar to a LOT of questions, but in no such way is it anything of a duplicate. This question is about string concatenation and writing better code less than it is for checking nil/zero.

Currently I have:

file.puts "cn: " + (var1.nil? ? "UNKNOWN" : var1)

Which works fine, but doesn't look good. What is a better way to write this in ruby so that I am checking for nil and not concatenating it

Radius answered 3/2, 2010 at 21:8 Comment(0)
R
45

You can do this:

file.puts "cn: " + (var1 || "UNKNOWN")

or, identically if you prefer:

file.puts "cn: " + (var1 or "UNKNOWN")

or my favourite, which I think is the most idiomatic ruby:

file.puts "cn: #{var1 or 'unknown'}"
Rhyton answered 3/2, 2010 at 21:12 Comment(1)
if var1 is false, you'll see 'unknown' instead.Longing
M
22

Use join to add the strings which may be nil.

The join will not complain if there is a nil

For example:

["a","b",nil,"c"].join("") 
#=> abc

However, if you are joining with anything but a blank string, like an underscore, you will get a join String for the nil value:

["a","b",nil,"c"].join("_")
#=> a_b__c

To fix this, use .compact to remove the nil values from the Array before joining:

["a","b",nil,"c"].compact.join("_")
#=> a_b_c
Moule answered 7/1, 2015 at 3:21 Comment(3)
This is true only when joining by an empty ["a","b",nil,"c"].join("_") => "a_b__c" (notice the double underscore after 'b')Kovach
@Kovach ["a","b",nil,"c"].compact.join("_") solves the problemJudyjudye
@Judyjudye Thanks. Updated the answer to reflect this.Depute
C
7

Using ruby 2.4.1, to_s resolves for both nil and "Hello". So var1.to_s should suffice.

2.4.1 :058 > nil.to_s
 => "" 
2.4.1 :059 > "hello".to_s
 => "hello" 
Catholicism answered 29/11, 2017 at 23:31 Comment(0)
B
4

I would do what Peter suggested, assuming that false wasn't a valid value for var1, and var1 was guaranteed to be nil or a string. You could also extract that logic into a function:

def display_value(var)
  (var || "UNKNOWN").to_s # or (var.nil? ? "UNKNOWN" : var.to_s) if 'false' is a valid value
end

file.puts "cn: " + display_value(var1)

to_s is only necessary if var1 isn't guaranteed to be nil or a string. Alternatively, if you do:

file.puts "cn: #{display_value(var1)}"

it will do an implicit to_s on the result of display_value

Boring answered 3/2, 2010 at 21:26 Comment(0)
K
1
file.puts( "cn:" + (var1 || "UNKNOWN" ))
Kenosis answered 3/2, 2010 at 21:13 Comment(0)
R
1

Since the "cn: " part is purely aesthetical and therefore (more?) subject to change to meet future presentation guidelines, I would recommend using join;

file.puts(["cn", (var1 || "UNKNOWN")].join(": ")

Perhaps as a function, as mentioned earlier - semantics are the same, only method names/keywords have changed;

def value_or_unknown(value, attribute = nil)
  [attribute, (value or "UNKNOWN")] * ": "
end
Resourceful answered 20/6, 2010 at 1:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.