Ruby 3.3 fixes duplicate keyword argument warning bug

Ruby, known for its elegant syntax and developer-friendly features, encountered an intriguing bug in its handling of keyword arguments.

This bug, residing deep within the parsing mechanism, led to inconsistent warnings when developers attempted to use duplicate keyword arguments.

In this blog post, we will explore the bugfix and its impact on previous versions of Ruby.

Before

In prior versions of Ruby, the behavior surrounding duplicate keyword arguments was not always predictable.

We can illustrate this by the following example.

def greet(message:, recipient:)
  puts "#{message}, #{recipient}!"
end

greet(message: "Hello", recipient: "Jon", message: "Hi")

The above snippet will give Hi, Jon! as output but will also throw a warning -

warning: key :message is duplicated and overwritten on line 5

Now, lets take another example to observe the bug

def greet(message:, recipient:)
  puts "#{message}, #{recipient}!"
end

options = { recipient: "Jon" }

greet(message: "Hello", **options, message: "Hi")

The above snippet just diplays Hi, Jon! as output and doesn’t throw a warning for duplicate keyword message.

After

The bugfix, implemented in Ruby 3.3, addressed scenarios like the one described above, ensuring warnings were consistently generated for duplicate keyword arguments.

Let’s confirm the same by running the above snippet in Ruby 3.3

def greet(message:, recipient:)
  puts "#{message}, #{recipient}!"
end

options = { recipient: "Jon" }

greet(message: "Hello", **options, message: "Hi")
warning: key :message is duplicated and overwritten on line 7

To know more about this bugfix, please refer to this PR

Need help on your Ruby on Rails or React project?

Join Our Newsletter