Re: Metaprogrammatically defining methods with required, optional, required keyword and optional keyword parameters?
John Carter <[email protected]> Tue, 18 Jun 2019 12:19:52 +1200
| Newsgroups | gmane.comp.lang.ruby.general |
|---|---|
| Message-ID | <CAFD1m3HnQ0JNeUsNKugMMPFKL1SNYYetBbaUYts9jJjxRgkaJQ@mail.gmail.com> |
Ah. Wait, no. That won't quite work.
Because I still cannot programatically create that block at run time.
ie. If instead of giving the parameter list at ruby invocation time,.... I
want at run time to do something very similar to the Struct facility...
def SmartStruct.create_struct_class(
require_list, # Ordered Hash of positional parameter to expected
class.
defaults: {}, # Hash of sym: default_value
required_keyword_list: {}, # Hash of sym: expected class
keyword_list: {}, # Map from keyword sym to default
accessors: []) # Array of syms to provide accessors for
I then want to generate a Class that will have the specified parameters and
defaults and will type check them at run time.
On Mon, Jun 17, 2019 at 6:58 PM Marvin Gülker <[email protected]>
wrote:
> 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>
>
--
John Carter
Phone : (64)(3) 358 6639
Tait Electronics
PO Box 1645 Christchurch
New Zealand
--
This Communication is Confidential. We only send and receive email on the
basis of the terms set out at www.taitradio.com/email_disclaimer
<http://www.taitradio.com/email_disclaimer>
Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>