Skip to content

ArgumentError: unknown keyword for required keyword argument #8577

@davishmcclurg

Description

@davishmcclurg

I'm seeing unexpected ArgumentError: unknown keyword errors for required keyword arguments when the method has an another optional keyword argument with a default statement (and the optional argument comes before the required one). Here's an example:

puts "RUBY_VERSION: #{RUBY_VERSION}"
puts "JRUBY_VERSION: #{JRUBY_VERSION}" if defined?(JRUBY_VERSION)

def example(optional: (default = true), required:)
  return required, optional, default
end

begin
  puts example(required: true).to_s
rescue => e
  puts e
end

begin
  puts example(required: true, optional: true).to_s
rescue => e
  puts e
end

Running it with JRuby:

% bin/jruby example.rb
RUBY_VERSION: 3.1.4
JRUBY_VERSION: 9.4.10.0-SNAPSHOT
unknown keyword: :required
unknown keyword: :required

And with regular Ruby:

% ASDF_RUBY_VERSION=3.1.4 ruby example.rb
RUBY_VERSION: 3.1.4
[true, true, true]
[true, true, nil]
% ASDF_RUBY_VERSION=3.4.1 ruby example.rb
RUBY_VERSION: 3.4.1
[true, true, true]
[true, true, nil]

Seems to work ok when the argument order is reversed:

def example(required:, optional: (default = true))
  return required, optional, default
end
% bin/jruby example.rb
RUBY_VERSION: 3.1.4
JRUBY_VERSION: 9.4.10.0-SNAPSHOT
[true, true, true]
[true, true, nil]

Or when the default value doesn't assign:

puts "RUBY_VERSION: #{RUBY_VERSION}"
puts "JRUBY_VERSION: #{JRUBY_VERSION}" if defined?(JRUBY_VERSION)

def example(optional: true, required:)
  return required, optional
end

begin
  puts example(required: true).to_s
rescue => e
  puts e
end

begin
  puts example(required: true, optional: true).to_s
rescue => e
  puts e
end
% bin/jruby example.rb
RUBY_VERSION: 3.1.4
JRUBY_VERSION: 9.4.10.0-SNAPSHOT
[true, true]
[true, true]

Environment Information

  • JRuby (built from master)

    % bin/jruby -v
    jruby 9.4.10.0-SNAPSHOT (3.1.4) 2025-01-19 97d04a0576 OpenJDK 64-Bit Server VM 21.0.5+11-LTS on 21.0.5+11-LTS +jit [arm64-darwin]
    
  • Operating system

    % uname -a
    Darwin Mac 24.2.0 Darwin Kernel Version 24.2.0: Fri Dec  6 19:02:12 PST 2024; root:xnu-11215.61.5~2/RELEASE_ARM64_T6031 arm64
    

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions