Although not exactly equivalent to your code, that's usually done with ||
operator.
<% optional_width ||= default_value %>
This is equivalent to optional_width = optional_width || default_value
. Due to shot-circuit evaluation, if optional_with
is "true", i.e. it's defined, and not nil
, the right-hand part becomes equal to it, and default_value
is not even computed. Otherwise, right-hand part would be equal to default_value
. That's essentially what you want to do.
Ok, I admit that it may not work for partial's locals. The particular situation I can imagine is that if in first render call the optional_width
variable was set to some value, and in the consequent call to render
it is not mentioned at all while keeping its value from the first run. Can't do such a check right now, though.