Module: TableSync::Utils

Defined in:
lib/table_sync/utils/proc_keywords_resolver.rb,
lib/table_sync/utils.rb

Overview

Problem:

> fn = proc { |first| puts first } > fn.call(:first, :second, :third) first

:second and :third was ignored. It’s ok.

> fn = proc { puts “test” } > fn.call(first: :first, second: :second, third: :third) test

And it’s ok.

> fn = proc { |&block| block.call } > fn.call(first: :first, second: :second, third: :third) { puts “test” } test

And this is ok too.

> fn = proc { |first:| puts first } > fn.call(first: :first, second: :second, third: :third) ArgumentError (unknown keywords: :second, :third)

¯_(ツ)_/¯

❤ Ruby ❤

Next code solve this problem for procs without word arguments, only keywords and block.

Defined Under Namespace

Classes: InterfaceChecker, ProcArray

Class Method Summary collapse

Class Method Details

.proc_keywords_resolver(&proc_for_wrap) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/table_sync/utils/proc_keywords_resolver.rb', line 37

def proc_keywords_resolver(&proc_for_wrap)
  available_keywords = proc_for_wrap.parameters
    .select { |type, _name| type == :keyreq }
    .map { |_type, name| name }

  proc do |keywords = {}, &block|
    proc_for_wrap.call(**keywords.slice(*available_keywords), &block)
  end
end