Project

General

Profile

Actions

Feature #17785

open

Allow named parameters to be keywords

Added by marcandre (Marc-Andre Lafortune) over 3 years ago. Updated 8 months ago.

Status:
Assigned
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 (1 open1 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

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0