Default values for block parameters

M

Matt Mower

Hi.

I want to dynamically create (i'm trying to use define_method) a
method using which has default values for it's parameters. However
when I try to do:

define_method "name" lambda { |x=1,y=2,z=3| f(x,y,z) }

I get a syntax error on the x=1 part. Am I just using the wrong
syntax? Or is it really not possible to create methods with default
parameter values dynamically?

Regards,

Matt
 
R

Robert Klemme

Matt Mower said:
Hi.

I want to dynamically create (i'm trying to use define_method) a
method using which has default values for it's parameters. However
when I try to do:

define_method "name" lambda { |x=1,y=2,z=3| f(x,y,z) }

I get a syntax error on the x=1 part. Am I just using the wrong
syntax? Or is it really not possible to create methods with default
parameter values dynamically?

I guess you will have to do

define_method( "name" ) { |*a| f(a[0]||1, a[1]||2, a[2]||3) }

Or something similar.

Kind regards

robert
 
M

Matt Mower

Hi Robert,

Matt Mower said:
Hi.

I want to dynamically create (i'm trying to use define_method) a
method using which has default values for it's parameters. However
when I try to do:

define_method "name" lambda { |x=1,y=2,z=3| f(x,y,z) }

I get a syntax error on the x=1 part. Am I just using the wrong
syntax? Or is it really not possible to create methods with default
parameter values dynamically?

I guess you will have to do

define_method( "name" ) { |*a| f(a[0]||1, a[1]||2, a[2]||3) }

Or something similar.

Ah, thank you -- I had thought of using || to get the defaults but not
to use |*a| to allow parameters to be passed, or not, which is the
vital bit.

Since this workaround is a touch unaesthetic I wonder whether there
was a particular reason for not allowing default values to be
specified for block parameters? Do you happen to know?

Thanks again.

M
 
R

Robert Klemme

Matt Mower said:
Hi Robert,

Matt Mower said:
Hi.

I want to dynamically create (i'm trying to use define_method) a
method using which has default values for it's parameters. However
when I try to do:

define_method "name" lambda { |x=1,y=2,z=3| f(x,y,z) }

I get a syntax error on the x=1 part. Am I just using the wrong
syntax? Or is it really not possible to create methods with default
parameter values dynamically?

I guess you will have to do

define_method( "name" ) { |*a| f(a[0]||1, a[1]||2, a[2]||3) }

Or something similar.

Ah, thank you -- I had thought of using || to get the defaults but not
to use |*a| to allow parameters to be passed, or not, which is the
vital bit.

Since this workaround is a touch unaesthetic I wonder whether there
was a particular reason for not allowing default values to be
specified for block parameters? Do you happen to know?

Dunno, but here's my guess: since blocks are usually called at a single
place in code (as opposed to a method) default parameters do not make much
sense generally.

Kind regards

robert
 
M

Mark Hubbart

Matt Mower said:
Hi Robert,

Hi.

I want to dynamically create (i'm trying to use define_method) a
method using which has default values for it's parameters. However
when I try to do:

define_method "name" lambda { |x=1,y=2,z=3| f(x,y,z) }

I get a syntax error on the x=1 part. Am I just using the wrong
syntax? Or is it really not possible to create methods with default
parameter values dynamically?

I guess you will have to do

define_method( "name" ) { |*a| f(a[0]||1, a[1]||2, a[2]||3) }

Or something similar.

Ah, thank you -- I had thought of using || to get the defaults but not
to use |*a| to allow parameters to be passed, or not, which is the
vital bit.

Since this workaround is a touch unaesthetic I wonder whether there
was a particular reason for not allowing default values to be
specified for block parameters? Do you happen to know?

Dunno, but here's my guess: since blocks are usually called at a single
place in code (as opposed to a method) default parameters do not make much
sense generally.

I think that's a good assessment... I think historically, they may
have been considered superfluous. But it could change in the future;
with the addition of &block syntax in a block's parameter list
(available now, in 1.9, and later, in 2.0), it might be that block
parameter lists are going to be made more consistent with method
parameter lists.

Maybe :)

cheers,
Mark
 
M

Matt Mower

I think that's a good assessment... I think historically, they may
have been considered superfluous. But it could change in the future;
with the addition of &block syntax in a block's parameter list
(available now, in 1.9, and later, in 2.0), it might be that block
parameter lists are going to be made more consistent with method
parameter lists.

Robert, Mark, thanks for your views. That was really helpful.

M
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top