Skip to content

Instantly share code, notes, and snippets.

@aks
Created November 7, 2022 18:25
Show Gist options
  • Save aks/ab398ee8d268a20f5565088beb016708 to your computer and use it in GitHub Desktop.
Save aks/ab398ee8d268a20f5565088beb016708 to your computer and use it in GitHub Desktop.
require 'awesome_print'
class Foo
def test(**kwargs)
ap kwargs
end
def self.tester(label, &block)
STDERR.printf "%12s: ", label
begin
block.call
warn "--> ok"
rescue ArgumentError
warn "--> ArgumentError"
end
end
end
kwargs = { a: 5, b: 6}
foo = Foo.new
Foo.tester("simple args") { foo.test(a: 1, b: 2) }
Foo.tester("hash arg") { foo.test({a: 3, b: 4}) }
Foo.tester("hash arg var") { foo.test(kwargs) }
Foo.tester("splat args") { foo.test(**kwargs) }
$ rbenv shell 2.7.5
$ ruby --version
ruby 2.7.5p203 (2021-11-24 revision f69aeb8314) [x86_64-darwin21]
$ ruby kwargs-test.rb >/dev/null
simple args: --> ok
hash arg: --> ok
hash arg var: --> ok
splat args: --> ok
$ rbenv shell 3.1.0
$ ruby --version
ruby 3.1.0p0 (2021-12-25 revision fb4df44d16) [x86_64-darwin21]
$ ruby kwargs-test.rb >/dev/null
simple args: --> ok
hash arg: --> ArgumentError
hash arg var: --> ArgumentError
splat args: --> ok
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment