Ruby: how to insert a conditional into a string concatenation

ruby

Solution

It is easier and more readable with interpolation instead of concatenation:

dear = ""

string = "hello#{ ' my dear' unless dear.empty? }, good morning!"

Problem

In a string concatenation, is it possible to include a conditional directly into the statement? In the example below, I want `"my dear"` to be concatenated only if the `dear` list is not empty. ``` dear = "" string = "hello" + " my dear" unless dear.empty? + ", good morning!" ``` But the result is an error: undefined method '+' for true I know the alternative would be to define an additional variable before this statement but I would like to avoid this.

Original source