Project

General

Profile

Actions

Feature #17938

open

Keyword alternative for boolean positional arguments

Added by matheusrich (Matheus Richard) over 3 years ago. Updated over 3 years ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:104169]

Description

Some Ruby methods accept optional boolean arguments. This kind of parameter is known to be confusing since you cannot tell just looking at the method call what the parameter mean. For example:

object.respond_to?(:symbol, false) # what does `false` mean?
object.methods(true) # what does `true` mean?

Now compare that to

object.respond_to?(:symbol, include_all: false)
object.methods(regular: true)
# or
object.methods(only_public: true)
# or
object.methods(include_all: false)

I know Matz doesn't like breaking changes, so maybe we could have both to not break current calls, but allow a nicer syntax in newer Ruby? I don't know the depths of the Ruby C implementation, so here's what I thought in plain Ruby:

def respond_to?(symbol, include_all_positional=false, include_all: nil)
  include_all ||= include_all_positional

  # ...
end

I'm willing to tackle this, if approved.


Files

Screenshot from 2021-06-06 11-37-44.png (5.63 KB) Screenshot from 2021-06-06 11-37-44.png Eregon (Benoit Daloze), 06/06/2021 09:38 AM
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0