Keyword arguments in a block, possible with zero arguments?

P

Peter Motzfeldt

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
 
A

ara.t.howard

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top