Re: Metaprogrammatically defining methods with required, optional, required keyword and optional keyword parameters?

Marvin Gülker <[email protected]> Mon, 17 Jun 2019 08:58:09 +0200
Newsgroups gmane.comp.lang.ruby.general
Message-ID <20190617065809.frr2ovii3mseswnb@atlantis>
Am 17. Juni 2019 um 10:20 Uhr +1200 schrieb John Carter:
> However modern ruby now has required parameters, default parameters,
> required keyword parameters and optional keyword parameters and an
> optional a block parameter.
> [...]
> Any idea how to define (at run time) the parameter list for a method?

Here's your example with define_method:

    define_method(:foo) do |bar, baz, but = 0, joke:, nojoke: 10, &block|
      p [bar, baz, but, joke, nojoke, block]
      block.call
    end

    foo(1, 2, joke: 50){puts "I'm a block!"}

Is that what you are looking for?

-- 
Blog: https://mg.guelker.eu

Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>