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


Groups > comp.lang.ruby > #7032

Keyword argument default if nil

Newsgroups comp.lang.ruby
Date 2014-10-04 07:41 -0700
Message-ID <c0f5cf3a-d03f-46dc-bae9-cef132892ba7@googlegroups.com> (permalink)
Subject Keyword argument default if nil
From Jo Potts <jopotts@gmail.com>

Show all headers | View raw


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

Back to comp.lang.ruby | Previous | NextNext 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