Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.ruby > #7036
| 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 <shortcutter@googlemail.com> |
| Newsgroups | comp.lang.ruby |
| Subject | Re: Keyword argument default if nil |
| Date | Wed, 08 Oct 2014 12:49:01 +0200 |
| Lines | 32 |
| Message-ID | <c9kj4uFb1b4U1@mid.individual.net> (permalink) |
| References | <c0f5cf3a-d03f-46dc-bae9-cef132892ba7@googlegroups.com> |
| 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 | <c0f5cf3a-d03f-46dc-bae9-cef132892ba7@googlegroups.com> |
| Xref | csiph.com comp.lang.ruby:7036 |
Show key headers only | View raw
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
Back to comp.lang.ruby | Previous | Next — Previous in thread | Find similar
Keyword argument default if nil Jo Potts <jopotts@gmail.com> - 2014-10-04 07:41 -0700 Re: Keyword argument default if nil Quintus <quintus@quintilianus.eu> - 2014-10-05 10:26 +0200 Re: Keyword argument default if nil Michael Uplawski <michael.uplawski@uplawski.eu> - 2014-10-07 13:33 +0200 Re: Keyword argument default if nil Robert Klemme <shortcutter@googlemail.com> - 2014-10-08 12:49 +0200
csiph-web