Project

General

Profile

Actions

Feature #17785

open

Allow named parameters to be keywords

Added by marcandre (Marc-Andre Lafortune) about 1 year ago. Updated 5 months ago.

Status:
Open
Priority:
Normal
Target version:
-
[ruby-core:103305]

Description

We should allow named parameters to be keywords and use add a trailing _ to the corresponding variable:

def check(arg, class:)
  arg.is_a?(class_)
end

check(42, class: Integer) # => true

Currently, if we want such an API we have to use **rest:

def check(arg, **rest)
  class_ = rest.fetch(:class) { raise ArgumentError('missing keyword: :class')}
  if rest.size > 1
    unknown = rest.keys - [:class]
    raise ArgumentError("unknown keyword(s): :#{unknown.join(', :')})
  end

  arg.is_a?(class_)
end

This is very verbose, much less convenient, much less readable, prevents steep from generating the proper signature, etc.

We should do the same for pattern match.


Related issues 2 (2 open0 closed)

Related to Ruby master - Feature #13207: Allow keyword local variable names like `class` or `for`FeedbackActions
Related to Ruby master - Feature #18402: Argument LabelsOpenActions
Actions

Also available in: Atom PDF