my solution is like
if (not (defined?(@results).nil?))
@results += "run"
else
@results = "run"
end
but I believe that there is something simpler ...
In JavaScript, we can assign strings to a variable and use concatenation to combine the variable to another string. To concatenate a string, you add a plus sign+ between the strings or string variables you want to connect. let myPet = 'seahorse'; console.
Create an Append Variable activity with UISelect the Append Variable activity on the canvas if it is not already selected, and its Variables tab, to edit its details. Select the variable for the Name property. Enter an expression for the value, which will be appended to the array in the variable.
You can use the + operator to append a string to another. In this case, a + b + c , creates a new string.
The “append to string variable” will always add to the end of the variable's string value, including spaces or any other character.
I would probably do it like this:
@results = @results.to_s + "run"
This works because NilClass
defines a #to_s
method that returns a zero-length String, and because instance variables are automatically initialized to nil.
You're right:
(@results ||= "") << "run"
To clarify, a || b
is a ? a : b
, meaning that it tries to use the value a
if a
is "truthy" (not false or nil) but uses b
if a
is "falsey". Using ||=
hence only updates a variable if the variable is nil. Then, <<
appends the string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With