Best way to set default for an optional local in a partial?

What’s the best way to set the default value of an optional local
variable in a partial template?

In the past I’ve used

<% foo = default_value unless (defined? foo) %>

But says not
to use (defined? foo) and instead use (local_assigns.has_key? :foo) thus

<% foo = default_value unless (local_assigns.has_key? :foo) %>

Some have suggested

<% foo ||= default_value %>

which has the benefit of compact syntax, but will override passed values
of nil and false. Some have also suggested

<% foo = default_value if local_assigns[:foo].nil? %>

which will override the passed value nil.

Are there other pros and cons to consider here? Are any of these
strictly preferred over the others



This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs