Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #7035

Re: Keyword argument default if nil

From Michael Uplawski <michael.uplawski@uplawski.eu>
Newsgroups comp.lang.ruby
Subject Re: Keyword argument default if nil
Date 2014-10-07 13:33 +0200
Organization news.babsi.de for open-news-network.org
Message-ID <slrnm37js3.2vn.michael.uplawski@drusus.uplawski.eu> (permalink)
References <c0f5cf3a-d03f-46dc-bae9-cef132892ba7@googlegroups.com>

Show all headers | View raw


Good afternoon, all.

On Sat, 4 Oct 2014 07:41:22 -0700 (PDT),
Jo Potts <jopotts@gmail.com> 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.
(...)
> # Proposal: Adding a '||=' causes baz to be set to "baz" if nil
> def foo(bar: 1, baz: ||= 2)

In addition to what Quintus has already written, I do not consider this
an *assignment*, but a control-structure, which usually is part of a
codeblock, not a method-definition.

I present my point of view but recognize, that it may just be an
expression of fear: 

The fact, that languages (like say... Ruby) allow us to shorten code
should not lead us to stuff too much intelligence into declarations or
definitions. I prefer that the nifty algorithms stay entire/undevided, 
inside the code-block and not /evenly spread/ on method-signature and
method-body.

I consider your example method quite well-written. You can assign
nil to baz in the parameter-list, but for documentation-purposes, your
version looks *better* because it *is* : 
> def foo(bar: 1, baz: 2)
>   baz ||= 2 
>   puts "#{bar}#{baz}"
> end

Cheerio,

Michael
-- 
GnuPG/OpenPGP  4096R/3216CF02 2013-11-15 [expires: 2015-11-15]
sub   4096R/2751C550 2013-11-15 [expires: 2015-11-15]

Back to comp.lang.ruby | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

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