0

In Ruby 3, the new ... syntax was introduced, allowing constructs like these:

def locked_run(...)
  lock
  run(...)
  unlock
end

This is documented here: https://rubyreferences.github.io/rubychanges/3.0.html

After this discussion (https://bugs.ruby-lang.org/issues/16378), it was decided to let positional arguments in as well:

  def block_section(name, ...)
    section(name, true, ...)
  end

However, the following still leads to a syntax error:

  def block_section(...)
    section(block_section: true, ...)
  end

Why are positional arguments allowed with ... but named arguments are not?

Kalsan
  • 822
  • 1
  • 8
  • 19
  • This isn't **exactly** an answer to your question, but could you not specify `**options` as a method parameter here, and `**options.merge(block_section: true)` below? – Tom Lord Jun 03 '21 at 10:11
  • This wouldn't work if your method is also potentially expecting to receive positional arguments, though. – Tom Lord Jun 03 '21 at 10:12
  • 1
    In the linked discussion Matz also [explains](https://bugs.ruby-lang.org/issues/16378#note-8) the reason for this decision, namely to support `send` and `method_missing`. – Stefan Jun 03 '21 at 10:20

1 Answers1

2
section(block_section: true, ...)

This wouldn't be the correct syntax anyway. Looking at how Ruby does things, it expects named arguments (i.e., hash) to be the last arguments after the normal ones.

def section(a,b,c,d: false)
  puts "#{a} #{b} #{c} #{d}"
end

def old_style_block_section(*args)
  section(*args,d: true)
  
  #=> syntax error, unexpected *, expecting ')'
  section(d: true,*args)
end

section(1,2,3,d: true)
old_style_block_section(1,2,3)

#=> syntax error, unexpected ',', expecting =>
section(d: true,1,2,3)

So what you would need is this:

section(..., block_section: true)

However, this still doesn't work, as it hasn't been implemented as part of the Ruby language grammar for the new ... currently.

If you want this, I'd suggest creating an issue (or emailing or something else) to request it.

esotericpig
  • 282
  • 1
  • 3
  • 14