Skip to content

Instantly share code, notes, and snippets.

@havenwood
Created November 7, 2019 06:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save havenwood/2b8ec33e743917678769ce3b4ae899f1 to your computer and use it in GitHub Desktop.
Save havenwood/2b8ec33e743917678769ce3b4ae899f1 to your computer and use it in GitHub Desktop.
An answer to crella133's question in #ruby IRC
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