Forum: Ruby Keyword arguments in a block, possible with zero arguments?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Cc7eee0b959bc8031e6549a34fa1a711?d=identicon&s=25 Peter Nicolai Motzfeldt (tcb)
on 2007-03-13 13:47

I am trying to create a method dynamically that has keyword arguments.
The variable properties is a hash like {:text => "Text", :something =>

My problem is that I want the method created to be able to take 0 or
more arguments. I know how to do this with an array, but is it possible
to do this with an hash and keyword arguments?

I mean to write something else than |options| that makes the method able
to take 0 arguments as well as more arguments?

# This method is in a class
def self.add_method(name, &block )
    define_method(name, &block)

# The method add_method is called on an instance of the class
instance_of_generated_class.add_method(methodName) do
    options = properties.merge(options)

    #some code
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 unknown (Guest)
on 2007-03-13 14:34
(Received via mailing list)
On Tue, 13 Mar 2007, Peter Motzfeldt wrote:

> I mean to write something else than |options| that makes the method able
>    options = properties.merge(options)
>    #some code
> end

def self.add_method name, &block
   define_method name do |*argv|
     options = argv.shift || {}
     options = properties.merge(options)

     # some code


This topic is locked and can not be replied to.