Actions
Feature #11361
openproposal for easy method to nil-guard for generated variable name.
Feature #11361:
proposal for easy method to nil-guard for generated variable name.
Status:
Open
Assignee:
-
Target version:
-
Description
It's easy to 'nil-guard' for normal variable.
but it's not simple for generated variable name.
def user(id)
variable_name = "@user_#{id}"
instance_variable_set(variable_name, User.find(id)) unless instance_variable_defined?(variable_name)
instance_variable_get(variable_name)
end
I want to write it like this.
it can be implemented by this code.
Updated by marcandre (Marc-Andre Lafortune) about 11 years ago
Updated by jwmittag (Jörg W Mittag) over 10 years ago
masaki yamada wrote:
It's easy to 'nil-guard' for normal variable.
but it's not simple for generated variable name.
Note that the two examples you gave are not equivalent: the first assigns to the variable if it evaluates to false or nil regardless of whether it is defined or not, the second only assigns if it is undefined. Which one of the two do you want?
Actions