memoit
memoit copied to clipboard
Interesting behavior on 2.7 with implicit hash conversion and keyword arguments
Hi! This PR is a discussion PR, opened mostly for the benefit of others running into the same issue. This problem also goes away in Q1 2023 when Ruby 2.x is EOL.
In short, this PR fails on Ruby 2.7.
The conditions for failure are:
- Using Ruby 2.x
- Single-argument method (probably)
- Parameter passed responds to
to_hash
Since memoit defines the memoized method with the following parameter list:
define_method(…) do |*args, **kwargs, &block|
The single parameter is automatically converted to a hash.
A minimal example would be:
class Foo; def to_hash; { x: "Haha" }; end; end
def foo(*a, **b)
p [a, b]
end
foo(Foo.new) # => [[], {:x=>"Haha"}]
p RUBY_VERSION # => 2.7.x
I'm not sure how to fix this, but I think I'd like to know how? I can't investigate further today, so this PR is really just a PSA.
However, the other side of me says "just use 0.3 if you're on Ruby 2.7" since this is a non-issue in Ruby 3.x :D