Define_method with default parameters

how can i use define_method to assign default parameters?

I want to do this:

a_helper_module.module_eval do

  define_method(:method_name) do |parameter, parameter2= "default"|
    puts parameter
    puts parameter2
  end

end

But parameter2= "default" is not a valid!

Thanks!

···

--
Posted via http://www.ruby-forum.com/.

Not sure if this is the most elegant way, but:

  define_method(:method_name) do |*args|
    parameter, parameter2 = *args
    parameter2 ||= 'default'
    puts parameter
    puts parameter2
  end

···

On Aug 9, 11:40 am, Emmanuel Oga <oga_emmanuel_...@yahoo.com.ar> wrote:

how can i use define_method to assign default parameters?

Emmanuel Oga wrote:

how can i use define_method to assign default parameters?

I want to do this:

a_helper_module.module_eval do
  define_method(:method_name) do |parameter, parameter2= "default"|
    puts parameter
    puts parameter2
  end
end

Why don't you just use the regular "def"? Am I missing something?

Daniel

mmmm better than my method! :slight_smile: :

        eval <<-EOMETHDEF
        def method_name parameter, parameter2= 'default'
          puts parameter
          puts parameter2
        end
        EOMETHDEF

thanks.

Gordon Thiesfeld wrote:

···

On Aug 9, 11:40 am, Emmanuel Oga <oga_emmanuel_...@yahoo.com.ar> > wrote:

how can i use define_method to assign default parameters?

Not sure if this is the most elegant way, but:

  define_method(:method_name) do |*args|
    parameter, parameter2 = *args
    parameter2 ||= 'default'
    puts parameter
    puts parameter2
  end

--
Posted via http://www.ruby-forum.com/\.

Gordon Thiesfeld wrote:

  

how can i use define_method to assign default parameters?

Not sure if this is the most elegant way, but:

  define_method(:method_name) do |*args|
    parameter, parameter2 = *args
    parameter2 ||= 'default'
    puts parameter
    puts parameter2
  end

Has the addition of block argument defaults to the language been considered? There are a number of places (especially in define_method, Proc.new, and lambda) where it would come in handy. The syntax would of course be:

def foo
  yield 'from method'
end

foo do |x, y = 'from default'|
  puts x
  puts y
end

Which would output:

from method
from default

I've seen this come up enough that its addition would seem welcome. Are there arguments against it?

Tom

···

On Aug 9, 11:40 am, Emmanuel Oga <oga_emmanuel_...@yahoo.com.ar> > wrote:

i prefer

   define_method 'method_name' do |required, *optional|
     one, two, *ignored = *optional
   end

because you an error will be thrown if required is not passed and you don't risk slurping ten arguments into 'two'

alternatively just use a hash

   define_method 'method_name' do |required, *options|
     options = options.first || Hash.new
     foobar = options[:foobar]
   end

   method_name 'required'
   method_name 'required', :foobar => 42

kind regards.

a @ http://drawohara.com/

···

On Aug 9, 2007, at 11:20 AM, Gordon Thiesfeld wrote:

Not sure if this is the most elegant way, but:

  define_method(:method_name) do |*args|
    parameter, parameter2 = *args
    parameter2 ||= 'default'
    puts parameter
    puts parameter2
  end

--
we can deny everything, except that we have the possibility of being better. simply reflect on that.
h.h. the 14th dalai lama

Hi --

···

On Fri, 10 Aug 2007, Daniel DeLorme wrote:

Emmanuel Oga wrote:

how can i use define_method to assign default parameters?

I want to do this:

a_helper_module.module_eval do
  define_method(:method_name) do |parameter, parameter2= "default"|
    puts parameter
    puts parameter2
  end
end

Why don't you just use the regular "def"? Am I missing something?

In this particular example you could, but the issue of defaults for
block parameters is still a real one, for #define_method and other
cases.

David

--
* Books:
   RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242\)
   RUBY FOR RAILS (http://www.manning.com/black\)
* Ruby/Rails training
     & consulting: Ruby Power and Light, LLC (http://www.rubypal.com)

I believe this has come up *lots*. IIRC, the current response is that such
block defaults are not possible with the lexical parser / grammar ruby
currently uses.

···

On Thursday 09 August 2007 11:38:06 am Tom Werner wrote:

Gordon Thiesfeld wrote:
> On Aug 9, 11:40 am, Emmanuel Oga <oga_emmanuel_...@yahoo.com.ar> > > wrote:
>
>> how can i use define_method to assign default parameters?
>>
>>
>
> Not sure if this is the most elegant way, but:
>
> define_method(:method_name) do |*args|
> parameter, parameter2 = *args
> parameter2 ||= 'default'
> puts parameter
> puts parameter2
> end
>
>
>

Has the addition of block argument defaults to the language been
considered? There are a number of places (especially in define_method,
Proc.new, and lambda) where it would come in handy. The syntax would of
course be:

def foo
  yield 'from method'
end

foo do |x, y = 'from default'|
  puts x
  puts y
end

Which would output:

from method
from default

I've seen this come up enough that its addition would seem welcome. Are
there arguments against it?

Tom

--
Konrad Meyer <konrad@tylerc.org> http://konrad.sobertillnoon.com/

Hi --

Gordon Thiesfeld wrote:

how can i use define_method to assign default parameters?

Not sure if this is the most elegant way, but:

  define_method(:method_name) do |*args|
    parameter, parameter2 = *args
    parameter2 ||= 'default'
    puts parameter
    puts parameter2
  end

Has the addition of block argument defaults to the language been considered?

Yes, quite often :slight_smile:

There are a number of places (especially in define_method, Proc.new, and lambda) where it would come in handy. The syntax would of course be:

def foo
yield 'from method'
end

foo do |x, y = 'from default'|
puts x
puts y
end

Which would output:

from method
from default

I've seen this come up enough that its addition would seem welcome. Are there arguments against it?

The problem is with something like:

   m do |a, b = 1 | 2 | 3; end

you can't tell which | is doing what.

David

···

On Fri, 10 Aug 2007, Tom Werner wrote:

On Aug 9, 11:40 am, Emmanuel Oga <oga_emmanuel_...@yahoo.com.ar> >> wrote:

--
* Books:
   RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242\)
   RUBY FOR RAILS (http://www.manning.com/black\)
* Ruby/Rails training
     & consulting: Ruby Power and Light, LLC (http://www.rubypal.com)

Hi --

···

On Fri, 10 Aug 2007, ara.t.howard wrote:

On Aug 9, 2007, at 11:20 AM, Gordon Thiesfeld wrote:

Not sure if this is the most elegant way, but:

define_method(:method_name) do |*args|
   parameter, parameter2 = *args
   parameter2 ||= 'default'
   puts parameter
   puts parameter2
end

i prefer

define_method 'method_name' do |required, *optional|
  one, two, *ignored = *optional
end

because you an error will be thrown if required is not passed and you don't risk slurping ten arguments into 'two'

You can do that just with the comma:

   one, two, = *optional

David

--
* Books:
   RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242\)
   RUBY FOR RAILS (http://www.manning.com/black\)
* Ruby/Rails training
     & consulting: Ruby Power and Light, LLC (http://www.rubypal.com)

dblack@rubypal.com wrote:

Hi --

Gordon Thiesfeld wrote:

how can i use define_method to assign default parameters?

Not sure if this is the most elegant way, but:

  define_method(:method_name) do |*args|
    parameter, parameter2 = *args
    parameter2 ||= 'default'
    puts parameter
    puts parameter2
  end

Has the addition of block argument defaults to the language been considered?

Yes, quite often :slight_smile:

There are a number of places (especially in define_method, Proc.new, and lambda) where it would come in handy. The syntax would of course be:

def foo
yield 'from method'
end

foo do |x, y = 'from default'|
puts x
puts y
end

Which would output:

from method
from default

I've seen this come up enough that its addition would seem welcome. Are there arguments against it?

The problem is with something like:

  m do |a, b = 1 | 2 | 3; end

you can't tell which | is doing what.

David

That's never stopped Ruby from doing other things on a single line. Single line 'if' statements need a 'then' or a semicolon (being syntax errors otherwise).

if x | y; 'foo'; end

Or consider the following single line:

x = 10 - 5 - 2

Perfectly valid, but wait! What I really meant was:

x = 10 - 5; -2

The onus is on the programmer to write code that works in the face of possibly ambiguous syntax.

Simply require a semicolon in your example case and there's no more problem:

m do |a, b = 1|; 2 | 3; end

or

m do |a, b = 1 | 2|; 3; end

Just because a certain functionality *might* produce ambiguous code seems a poor reason to exclude it from consideration!

Tom

···

On Fri, 10 Aug 2007, Tom Werner wrote:

On Aug 9, 11:40 am, Emmanuel Oga <oga_emmanuel_...@yahoo.com.ar> >>> wrote:

i used to use that, but people 'correct it' to

   one, two = *optional

and lo it works so long as there are two, then, when there are three it blows up so i've take to 'doccumenting' it with '*ignored'

paranoid i guess :wink:

a @ http://drawohara.com/

···

On Aug 9, 2007, at 2:42 PM, dblack@rubypal.com wrote:

  one, two, = *optional

--
we can deny everything, except that we have the possibility of being better. simply reflect on that.
h.h. the 14th dalai lama

Hi --

···

On Fri, 10 Aug 2007, ara.t.howard wrote:

On Aug 9, 2007, at 2:42 PM, dblack@rubypal.com wrote:

one, two, = *optional

i used to use that, but people 'correct it' to

one, two = *optional

and lo it works so long as there are two, then, when there are three it blows up so i've take to 'doccumenting' it with '*ignored'

paranoid i guess :wink:

Or maybe I'm too sanguine :slight_smile:

David

--
* Books:
   RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242\)
   RUBY FOR RAILS (http://www.manning.com/black\)
* Ruby/Rails training
     & consulting: Ruby Power and Light, LLC (http://www.rubypal.com)