Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.ruby > #7036
| From | Robert Klemme <shortcutter@googlemail.com> |
|---|---|
| Newsgroups | comp.lang.ruby |
| Subject | Re: Keyword argument default if nil |
| Date | 2014-10-08 12:49 +0200 |
| Message-ID | <c9kj4uFb1b4U1@mid.individual.net> (permalink) |
| References | <c0f5cf3a-d03f-46dc-bae9-cef132892ba7@googlegroups.com> |
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