I have a case where, either variable is defined, or variable is nil and alternative_variable is defined. I need to implement logic into interpolation to choose the variable among these that is defined. When I do:
some_value: "#{ variable || alternative_variable }"
when variable is defined, everything works fine. The moment variable is not defined (and alternative_variable is present), I get the following error:
undefined local variable or method `variable' for #<#Class:0x007fc3c021bc28>:0x007fc3c006c030>
I’ve found a way to bypass the error by adding the following code block before interpolation:
<% variable ||= alternative_variable %>
But it feels dirty and not-rails-way. Is there a neat and tidy way to place logic inside interpolation directly?
This has nothing to do with rails, it’s just ruby. Try this: