13

Setting a default value for a splat argument gives an error:

1.9.3-p374 :001 > def a b, *c = nil
1.9.3-p374 :002?>   end
SyntaxError: (irb):1: syntax error, unexpected '=', expecting ';' or '\n'
def a b, *c = nil
             ^
    from /Users/me/.rvm/rubies/ruby-1.9.3-p374/bin/irb:16:in `<main>'

Some variations I tried that don't work either:

1.9.3-p374 :003 > def a b, *c = []
1.9.3-p374 :005 > def a b, (*c) = nil
1.9.3-p374 :007 > def a b, (*c = [])
1.9.3-p374 :009 > def a b, (*c = [1,2,3])
1.9.3-p374 :011 > def a b, *c = [1,2,3]

I don't see an indeterminacy issue here, so it seems like it should be possible.

Related: Why non-explicit splat param plus default param is wrong syntax for method definition in Ruby 1.9?

Community
  • 1
  • 1
jordanpg
  • 6,386
  • 4
  • 46
  • 70

4 Answers4

6

Your attempted usage is counter the conventions around splat usage. Splats are supposed (at least in Ruby) to take up all extra (0, 1 or more) values.

If you know that you want the second value in your method arguments list to have a default value, you could take it out of the splat and list it just before the splat with a default value like this:

def a b, c=nil, *d 
  # rest of code omitted
end

EDIT: To make the answer to your question of why it doesn't work perfectly clear. It's a design decision by the language designer. Matz never intended the splat operator to work with defaults. This seems pretty sensible to me since it is intended to be used for catching an indeterminate number of variables and because the method I described reads more clearly than the possibilities you described and because all of the problems your examples solve are solvable in other ways.

vlasits
  • 2,215
  • 1
  • 15
  • 27
  • 1
    +1 This is the right way to go about this. If `c` is that special, promote it to its own parameter entry, and don't play games trying to extract it from the splatted variable. – the Tin Man Apr 03 '13 at 20:47
5

You could set the default value in the method itself knowing that the default splat returns an empty array.

def test(a, *b)
  b = "default b" if b == [] # edited as per Tin Man's recommendation
  puts [a, b].inspect
end

test("Test", 1, 2)
# => ["Test", [1, 2]]
test("Test")
# => ["Test", "default b"]

In Rails, you could check for b.present? as an empty array is considered blank. Hope that helps.

Thomas Klemm
  • 10,678
  • 1
  • 51
  • 54
  • `b == [] and b = "default b"` is not recommended for Ruby style. Use `b = "default b" if b == []`. – the Tin Man Apr 03 '13 at 20:45
  • Thanks, I incorporated your suggestion. Is there a technical reason behind this? – Thomas Klemm Apr 03 '13 at 20:49
  • 4
    It's a readability and maintenance issue. Using a boolean to join the conditional test and assignment is very much a C/Perl construct; It's terse, almost cryptic, without gaining appreciable, if any, speed. When writing in C or Perl it's expected, almost macho, to be able to write like that, but the Ruby way is to give a bit on terseness for more readability. Also, if you use `and`, and then add additional tests or assignments, order of precedence can bite you, which can be very hard to track down. Using `&&` avoids the precedence problem a lot of times, but doesn't increase readability. – the Tin Man Apr 03 '13 at 23:14
  • Thanks a lot @theTinMan for elaborating! – Thomas Klemm Apr 04 '13 at 07:51
0

A splat argument defaults to an empty array, without your having to do anything special.

def a(b, *c)
  c
end

a("foo")
#=> []
Wally Altman
  • 3,535
  • 3
  • 25
  • 33
0

This is very convenient:

def initialize(*response_names)
  @response_names = response_names.presence || %w(default values)
end
Martin Streicher
  • 1,983
  • 1
  • 18
  • 18