Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.ruby > #7007
| From | Robert Klemme <shortcutter@googlemail.com> |
|---|---|
| Newsgroups | comp.lang.ruby |
| Subject | Re: Proposal for easier syntax for define-unless-already-defined pattern |
| Date | 2014-07-30 19:45 +0200 |
| Message-ID | <c3sp9mF41umU1@mid.individual.net> (permalink) |
| References | <fedb7003-f5e4-457b-9120-1ad937a9eedc@googlegroups.com> |
On 29.07.2014 18:40, Eugene Gilburg wrote: > Below pattern is very popular in Ruby, but does not work if the > method returns `nil` or `false`: > > @obj ||= some_expensive_method() > > For more consistent and efficient use of this pattern, you'd have to > write: > > if defined?(@obj) @obj else @obj = some_expensive_method() end > > Because above syntax is clunky, people tend not to use it, with the > less efficient ||= being far more common. Where is this less efficient? I don't think it is. > So what do you think of `?=` syntax below, to exactly replicate the > above code block? It should check not value of variable but only > whether the variable itself is defined: > > @obj ?= some_expensive_method() Operator ||= works remarkably good in practice and for the rare cases where #some_expensive_method returns nil or false the user can use the test with #defined? explicitly. Not sure whether that small benefit warrants a language change. Kind regards robert
Back to comp.lang.ruby | Previous | Next — Previous in thread | Find similar
Proposal for easier syntax for define-unless-already-defined pattern Eugene Gilburg <eugene.gilburg@gmail.com> - 2014-07-29 09:40 -0700
Re: Proposal for easier syntax for define-unless-already-defined pattern Eugene Gilburg <eugene.gilburg@gmail.com> - 2014-07-29 09:44 -0700
Re: Proposal for easier syntax for define-unless-already-defined pattern Robert Klemme <shortcutter@googlemail.com> - 2014-07-30 19:41 +0200
Re: Proposal for easier syntax for define-unless-already-defined pattern Robert Klemme <shortcutter@googlemail.com> - 2014-07-30 19:45 +0200
csiph-web