Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news-1.dfn.de!news.dfn.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Robert Klemme Newsgroups: comp.lang.ruby Subject: Re: Proposal for easier syntax for define-unless-already-defined pattern Date: Wed, 30 Jul 2014 19:41:26 +0200 Lines: 28 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net jZca+TW3ibwojTmt/9UQ5QjcA4djNiyVOLFYqnZuV/Lbn2B2w= Cancel-Lock: sha1:8OispofuRabLf84y4KI7O8kF+Qo= User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.0 In-Reply-To: Xref: csiph.com comp.lang.ruby:7006 On 29.07.2014 18:44, Eugene Gilburg wrote: > Forgot to add, the `?=` syntax should be context-sensitive to > primitive types. E.g. > > some_hash[:some_member] ?= 'some_value' > > Should check for `some_hash.key?(:some_value)` That is unlikely to happen. The compiler cannot reliably determine that some_hash is a Hash for all cases and hence method #key? must be used instead of #defined?. > Which will be assigned if the key does not exist, but not be assigned > if the key exists but has `false` or `nil` value (another difference > with the `||=` pattern). The only way to make that happen is to require the object to also implement #key? besides of #[] and #[]= - but then for example Array would need this as well. Which prompts the question what does #key? return for in index less than the Array's length where the value stored here is nil? Kind regards robert