Pramodh KP
Pramodh KP

Reputation: 199

Ruby parameters SyntaxError with varargs

    def a(b: 88, c: 97)
      puts b
      puts c
    end

The above code works. But,

def a(b: 88, c: 97, *c)
  puts b
  puts c
end

Throws a syntax error. Can anyone point me to the right documentation that explains it?

Upvotes: 1

Views: 194

Answers (1)

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230531

Positional arguments go first in a method signature. Named arguments go last.

This will work better, but you still have a duplicate parameter name, which is not allowed.

def a(*c, b: 88, c: 97)
  puts b
  puts c
end
# ~> -:1: duplicated argument name
# ~> def a(*c, b: 88, c: 97)
# ~>                    ^

Great answers with more info: Mixing keyword with regular arguments in Ruby?

Upvotes: 5

Related Questions