Skip to content

Instantly share code, notes, and snippets.

@rattrayalex
Created June 1, 2022 21:00
Show Gist options
  • Save rattrayalex/2abdf9a722801b0b3ca04a824a5577e0 to your computer and use it in GitHub Desktop.
Save rattrayalex/2abdf9a722801b0b3ca04a824a5577e0 to your computer and use it in GitHub Desktop.
Fiddling with kwargs in Ruby 3 at https://try.ruby-lang.org/
puts RUBY_VERSION
def foo(params={}, x: nil, **kwargs)
puts({params: params, x: x, kwargs: kwargs})
end
foo(a: 1)
foo(a: 1, x: 2)
foo(x: 1)
foo({a: 1})
foo({a: 1}, x: 1)
foo(a: 1, x: 1, b: 2)
foo({a: 1}, x: 1, b: 2)
3.1.0
{"params"=>{}, "x"=>nil, "kwargs"=>{"a"=>1}}
{"params"=>{}, "x"=>2, "kwargs"=>{"a"=>1}}
{"params"=>{}, "x"=>1, "kwargs"=>{}}
{"params"=>{}, "x"=>nil, "kwargs"=>{"a"=>1}}
{"params"=>{"a"=>1}, "x"=>1, "kwargs"=>{}}
{"params"=>{}, "x"=>1, "kwargs"=>{"a"=>1, "b"=>2}}
{"params"=>{"a"=>1}, "x"=>1, "kwargs"=>{"b"=>2}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment