Skip to content

Instantly share code, notes, and snippets.

@bricker
Created September 13, 2022 21:57
Show Gist options
  • Save bricker/9d9bb8c19aa6376684429826e4c6e114 to your computer and use it in GitHub Desktop.
Save bricker/9d9bb8c19aa6376684429826e4c6e114 to your computer and use it in GitHub Desktop.

In Ruby 2.7, an explicit Hash (with curly braces) as the last argument is implicitly converted to kwargs.

In Ruby 3.0+, an explicit Hash (with curly braces) as the last argument is treated as a Hash literal, and can't be parsed as kwargs.

def test_kwargs_parsing(a, b, **kwargs)
print a, b, kwargs
end
# Ruby 2.7: 1, 2, {:c=>3, :d=>4}
# Ruby 3.1: 1, 2, {:c=>3, :d=>4}
test_kwargs_parsing(1, 2, c:3, d:4)
# Ruby 2.7: 1, 2, {:c=>3,:d=>4}
# Ruby 3.0+: wrong number of arguments (given 3, expected 2) (ArgumentError)
test_kwargs_parsing(1, 2, {c:3, d:4})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment