Keyword arguments in a block, possible with zero arguments?

Discussion in 'Ruby' started by Peter Motzfeldt, Mar 13, 2007.

  1. Hi

    I am trying to create a method dynamically that has keyword arguments.
    The variable properties is a hash like {:text => "Text", :something =>
    "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)
    end

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

    #some code
    end

    --
    Posted via http://www.ruby-forum.com/.
    Peter Motzfeldt, Mar 13, 2007
    #1
    1. Advertising

  2. Peter Motzfeldt

    Guest

    On Tue, 13 Mar 2007, Peter Motzfeldt wrote:

    > Hi
    >
    > I am trying to create a method dynamically that has keyword arguments.
    > The variable properties is a hash like {:text => "Text", :something =>
    > "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)
    > end
    >
    > # The method add_method is called on an instance of the class
    > instance_of_generated_class.add_method(methodName) do
    > |options|
    > 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
    end
    end

    regards.

    -a
    --
    be kind whenever possible... it is always possible.
    - the dalai lama
    , Mar 13, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Zhiqiang Ye
    Replies:
    53
    Views:
    10,213
    Dan Pop
    Jun 28, 2004
  2. Edward Diener
    Replies:
    14
    Views:
    4,875
    Josiah Carlson
    Apr 6, 2004
  3. Gerard Flanagan
    Replies:
    3
    Views:
    426
    Terry Hancock
    Nov 19, 2005
  4. morrell
    Replies:
    1
    Views:
    933
    roy axenov
    Oct 10, 2006
  5. Christopher Benson-Manica

    Doubles and zero/negative zero

    Christopher Benson-Manica, Jun 30, 2004, in forum: C Programming
    Replies:
    4
    Views:
    656
    Walter
    Jul 1, 2004
Loading...

Share This Page