Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Keyword arguments in a block, possible with zero arguments?

Reply
Thread Tools

Keyword arguments in a block, possible with zero arguments?

 
 
Peter Motzfeldt
Guest
Posts: n/a
 
      03-13-2007
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/.

 
Reply With Quote
 
 
 
 
ara.t.howard@noaa.gov
Guest
Posts: n/a
 
      03-13-2007
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is it possible to use keyword arguments from C? Darren Dale Python 1 07-13-2009 01:47 PM
RE: keyword checker - keyword.kwlist Hamilton, William Python 4 05-13-2007 06:31 AM
keyword checker - keyword.kwlist tom@finland.com Python 6 05-10-2007 04:53 PM
zero new keyword decorator Roman Suzi Python 1 08-12-2004 07:46 PM
Difference between default arguments and keyword arguments Edward Diener Python 14 04-05-2004 11:26 PM



Advertisments