Created
November 7, 2022 18:25
-
-
Save aks/ab398ee8d268a20f5565088beb016708 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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