From: Robert Klemme Date: 2005-02-01T17:10:50+09:00 Subject: Re: Default values for block parameters "Matt Mower" schrieb im Newsbeitrag news:d563731905013110444211811e@mail.gmail.com... > Hi Robert, > > On Tue, 1 Feb 2005 03:30:46 +0900, Robert Klemme wrote: > > > > "Matt Mower" schrieb im Newsbeitrag > > news:d563731905013109382bbeef75@mail.gmail.com... > > > 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