Reputation: 4330
What I want to do is something like variable || "default string"
.
But this works only if variable
is nil and for some reason it's undefined.
Is it possible to set the default value to nil, or is there a better way to achieve this?
Upvotes: 0
Views: 273
Reputation: 51161
You can make profit of the fact that Ruby local variables evaluate to nil
by default with:
variable ||= 'default string'
Upvotes: 2
Reputation: 168101
You can do it like this:
defined?(variable) ? variable : "default string"
Upvotes: 1