[#73707] [Ruby trunk Misc#12004] Code of Conduct — hanmac@...
Issue #12004 has been updated by Hans Mackowiak.
3 messages
2016/02/05
[#73730] [Ruby trunk Feature#12034] RegExp does not respect file encoding directive — nobu@...
Issue #12034 has been updated by Nobuyoshi Nakada.
3 messages
2016/02/07
[#73746] [Ruby trunk Feature#12034] RegExp does not respect file encoding directive — nobu@...
Issue #12034 has been updated by Nobuyoshi Nakada.
3 messages
2016/02/09
[#73919] [Ruby trunk Feature#11262] Make more objects behave like "Functions" — Ruby-Lang@...
Issue #11262 has been updated by J旦rg W Mittag.
3 messages
2016/02/22
[#74019] [Ruby trunk Bug#12103][Rejected] ruby process hangs while executing regular expression. — duerst@...
Issue #12103 has been updated by Martin D端rst.
3 messages
2016/02/27
[ruby-core:73909] [Ruby trunk Feature#11361] proposal for easy method to nil-guard for generated variable name.
From:
Ruby-Lang@...
Date:
2016-02-21 12:01:02 UTC
List:
ruby-core #73909
Issue #11361 has been updated by J旦rg W Mittag.
masaki yamada wrote:
> It's easy to 'nil-guard' for normal variable.
> ~~~ruby
> def user
> @user ||= User.find(1)
> end
> ~~~
> but it's not simple for generated variable name.
> ~~~ruby
> 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
> ~~~
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?
----------------------------------------
Feature #11361: proposal for easy method to nil-guard for generated variable name.
https://bugs.ruby-lang.org/issues/11361#change-57068
* Author: masaki yamada
* Status: Open
* Priority: Normal
* Assignee:
----------------------------------------
It's easy to 'nil-guard' for normal variable.
~~~
def user
@user ||= User.find(1)
end
~~~
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.
~~~
def user(id)
instance_variable_get_or_set("@user_#{id}") { User.find(1) }
end
~~~
it can be implemented by this code.
~~~
def instance_variable_get_or_set(variable_name, &block)
instance_variable_set(variable_name, block.call) unless instance_variable_defined?(variable_name)
instance_variable_get(variable_name)
end
~~~
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>