Created
November 7, 2019 06:27
-
-
Save havenwood/2b8ec33e743917678769ce3b4ae899f1 to your computer and use it in GitHub Desktop.
An answer to crella133's question in #ruby IRC
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
class Foo | |
def initialize(a, b, c:) | |
puts 'Foo!' | |
end | |
end | |
class MakeItWork < Foo | |
def initialize(*args, **kwargs, &block) | |
parameters = method(__method__).super_method.parameters | |
required = parameters.count { |key, _value| key == :req } | |
keywords = parameters.filter_map do |key, value| | |
value if key == :keyreq | |
end | |
super(*args.first(required), **kwargs.slice(*keywords), &block) | |
end | |
end | |
MakeItWork.new 1, 2, 3, a: 42, b: 42, c: 5 | |
#>> Foo! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment