Sir l33tname
Sir l33tname

Reputation: 4330

erb layout variables are undefined and not nil

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

Answers (2)

Marek Lipka
Marek Lipka

Reputation: 51161

You can make profit of the fact that Ruby local variables evaluate to nil by default with:

variable ||= 'default string'

Upvotes: 2

sawa
sawa

Reputation: 168101

You can do it like this:

defined?(variable) ? variable : "default string"

Upvotes: 1

Related Questions