diff options
author | Jeremy Evans <code@jeremyevans.net> | 2019-09-21 09:03:36 -0700 |
---|---|---|
committer | Jeremy Evans <code@jeremyevans.net> | 2019-09-25 12:33:52 -0700 |
commit | 3b302ea8c95d34d5ef072d7e3b326f28a611e479 (patch) | |
tree | 5a0a5cadb3511d6a3ecf4f234abffecafbeec9d8 /test/test_delegate.rb | |
parent | 80b5a0ff2a7709367178f29d4ebe1c54122b1c27 (diff) | |
download | ruby-3b302ea8c95d34d5ef072d7e3b326f28a611e479.tar.gz |
Add Module#ruby2_keywords for passing keywords through regular argument splats
This approach uses a flag bit on the final hash object in the regular splat,
as opposed to a previous approach that used a VM frame flag. The hash flag
approach is less invasive, and handles some cases that the VM frame flag
approach does not, such as saving the argument splat array and splatting it
later:
ruby2_keywords def foo(*args)
@args = args
bar
end
def bar
baz(*@args)
end
def baz(*args, **kw)
[args, kw]
end
foo(a:1) #=> [[], {a: 1}]
foo({a: 1}, **{}) #=> [[{a: 1}], {}]
foo({a: 1}) #=> 2.7: [[], {a: 1}] # and warning
foo({a: 1}) #=> 3.0: [[{a: 1}], {}]
It doesn't handle some cases that the VM frame flag handles, such as when
the final hash object is replaced using Hash#merge, but those cases are
probably less common and are unlikely to properly support keyword
argument separation.
Use ruby2_keywords to handle argument delegation in the delegate library.
Diffstat (limited to 'test/test_delegate.rb')
-rw-r--r-- | test/test_delegate.rb | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/test/test_delegate.rb b/test/test_delegate.rb index 38e38ad781..9634681797 100644 --- a/test/test_delegate.rb +++ b/test/test_delegate.rb @@ -177,6 +177,25 @@ class TestDelegateClass < Test::Unit::TestCase assert_not_operator(s0, :eql?, "bar") end + def test_keyword_and_hash + foo = Object.new + def foo.bar(*args) + args + end + def foo.foo(*args, **kw) + [args, kw] + end + d = SimpleDelegator.new(foo) + assert_equal([[], {}], d.foo) + assert_equal([], d.bar) + assert_equal([[], {:a=>1}], d.foo(:a=>1)) + assert_equal([{:a=>1}], d.bar(:a=>1)) + assert_warn(/The last argument is used as the keyword parameter.* for `foo'/m) do + assert_equal([[], {:a=>1}], d.foo({:a=>1})) + end + assert_equal([{:a=>1}], d.bar({:a=>1})) + end + class Foo private def delegate_test_private |