Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Robert Klemme Newsgroups: comp.lang.ruby Subject: Re: Keyword argument default if nil Date: Wed, 08 Oct 2014 12:49:01 +0200 Lines: 32 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net PEssbBJkKv8EODPjQQuveQs5i4KRF426JAGyYNSjeUFxIP/oE= Cancel-Lock: sha1:VwESECi9VsA0ZyPMV+KwFWvLUyk= User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.1.2 In-Reply-To: Xref: csiph.com comp.lang.ruby:7036 On 04.10.2014 16:41, Jo Potts wrote: > There's not currently a way (unless I'm mistaken) to assign a > default value to a keyword argument if the value passed in is nil. > > For example: > > # Proposal: Adding a '||=' causes baz to be set to "baz" if nil > def foo(bar: 1, baz: ||= 2) > puts "#{bar}#{baz}" > end > > foo #=> "12" > foo(bar: nil, baz: nil) #=> "2" > > # The current way of doing this requires a duplication > # of the declaration of the default value. > def foo(bar: 1, baz: 2) > baz ||= 2 > puts "#{bar}#{baz}" > end What stops you from doing this? def foo(bar: 1, baz: nil) baz ||= 2 puts "#{bar}#{baz}" end Kind regards robert