# Define\_method with default parameters

**URL:** <https://www.ruby-forum.com/t/define-method-with-default-parameters/111676>\
**Category:** Ruby\
**Created:** [August 9, 2007, 6:40pm UTC](https://www.ruby-forum.com/t/define-method-with-default-parameters/111676 "2007-08-09T18:40:55Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![emmanueloga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ecae2f/32.png) [@emmanueloga](https://www.ruby-forum.com/u/emmanueloga)\
**Post date:** [August 9, 2007, 6:40pm UTC](https://www.ruby-forum.com/t/define-method-with-default-parameters/111676/1 "2007-08-09T18:40:55Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![emmanueloga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ecae2f/32.png) [@emmanueloga](https://www.ruby-forum.com/u/emmanueloga)\
**Post date:** [August 9, 2007, 7:21pm UTC](https://www.ruby-forum.com/t/define-method-with-default-parameters/111676/2 "2007-08-09T19:21:36Z")

</div>

On Aug 9, 11:40 am, Emmanuel O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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

---

<div class="post-metadata">

**Author:** ![emmanueloga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ecae2f/32.png) [@emmanueloga](https://www.ruby-forum.com/u/emmanueloga)\
**Post date:** [August 9, 2007, 8:13pm UTC](https://www.ruby-forum.com/t/define-method-with-default-parameters/111676/3 "2007-08-09T20:13:20Z")

</div>

mmmm better than my method! 🙂 :

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

```

thanks.

Gordon T. wrote:

> On Aug 9, 11:40 am, Emmanuel O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> 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

---

<div class="post-metadata">

**Author:** ![emmanueloga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ecae2f/32.png) [@emmanueloga](https://www.ruby-forum.com/u/emmanueloga)\
**Post date:** [August 9, 2007, 8:38pm UTC](https://www.ruby-forum.com/t/define-method-with-default-parameters/111676/4 "2007-08-09T20:38:45Z")

</div>

Gordon T. wrote:

> ```
> 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

---

<div class="post-metadata">

**Author:** ![emmanueloga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ecae2f/32.png) [@emmanueloga](https://www.ruby-forum.com/u/emmanueloga)\
**Post date:** [August 9, 2007, 8:47pm UTC](https://www.ruby-forum.com/t/define-method-with-default-parameters/111676/5 "2007-08-09T20:47:47Z")

</div>

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

> > define\_method(:method\_name) do |\*args|  
> > Has the addition of block argument defaults to the language been  
> > puts y  
> > Tom  
> > 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.

---

<div class="post-metadata">

**Author:** ![emmanueloga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ecae2f/32.png) [@emmanueloga](https://www.ruby-forum.com/u/emmanueloga)\
**Post date:** [August 9, 2007, 9:01pm UTC](https://www.ruby-forum.com/t/define-method-with-default-parameters/111676/6 "2007-08-09T21:01:49Z")

</div>

Hi –

On Fri, 10 Aug 2007, Tom W. wrote:

> > define\_method(:method\_name) do |\*args|  
> > Has the addition of block argument defaults to the language been considered?  
> > Yes, quite often 🙂

> 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

---

<div class="post-metadata">

**Author:** ![emmanueloga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ecae2f/32.png) [@emmanueloga](https://www.ruby-forum.com/u/emmanueloga)\
**Post date:** [August 9, 2007, 10:03pm UTC](https://www.ruby-forum.com/t/define-method-with-default-parameters/111676/7 "2007-08-09T22:03:55Z")

</div>

removed\_email\_address@domain.invalid wrote:

> > > >
> 
> 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

---

<div class="post-metadata">

**Author:** ![emmanueloga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ecae2f/32.png) [@emmanueloga](https://www.ruby-forum.com/u/emmanueloga)\
**Post date:** [August 9, 2007, 10:08pm UTC](https://www.ruby-forum.com/t/define-method-with-default-parameters/111676/8 "2007-08-09T22:08:56Z")

</div>

On Aug 9, 2007, at 11:20 AM, Gordon T. 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/](http://drawohara.com/)

---

<div class="post-metadata">

**Author:** ![emmanueloga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ecae2f/32.png) [@emmanueloga](https://www.ruby-forum.com/u/emmanueloga)\
**Post date:** [August 9, 2007, 10:44pm UTC](https://www.ruby-forum.com/t/define-method-with-default-parameters/111676/9 "2007-08-09T22:44:00Z")

</div>

Hi –

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

> > puts parameter2
> 
> 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

---

<div class="post-metadata">

**Author:** ![emmanueloga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ecae2f/32.png) [@emmanueloga](https://www.ruby-forum.com/u/emmanueloga)\
**Post date:** [August 9, 2007, 11:29pm UTC](https://www.ruby-forum.com/t/define-method-with-default-parameters/111676/10 "2007-08-09T23:29:07Z")

</div>

On Aug 9, 2007, at 2:42 PM, removed\_email\_address@domain.invalid 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 😉

a @ [http://drawohara.com/](http://drawohara.com/)

---

<div class="post-metadata">

**Author:** ![emmanueloga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ecae2f/32.png) [@emmanueloga](https://www.ruby-forum.com/u/emmanueloga)\
**Post date:** [August 9, 2007, 11:43pm UTC](https://www.ruby-forum.com/t/define-method-with-default-parameters/111676/11 "2007-08-09T23:43:10Z")

</div>

Hi –

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

> up so i’ve take to ‘doccumenting’ it with ‘\*ignored’
> 
> paranoid i guess 😉

Or maybe I’m too sanguine 🙂

David

---

<div class="post-metadata">

**Author:** ![emmanueloga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ecae2f/32.png) [@emmanueloga](https://www.ruby-forum.com/u/emmanueloga)\
**Post date:** [August 10, 2007, 1:56am UTC](https://www.ruby-forum.com/t/define-method-with-default-parameters/111676/12 "2007-08-10T01:56:19Z")

</div>

Hi –

On Fri, 10 Aug 2007, Daniel DeLorme wrote:

> > 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

---

<div class="post-metadata">

**Author:** ![emmanueloga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ecae2f/32.png) [@emmanueloga](https://www.ruby-forum.com/u/emmanueloga)\
**Post date:** [August 10, 2007, 1:22am UTC](https://www.ruby-forum.com/t/define-method-with-default-parameters/111676/13 "2007-08-10T01:22:17Z")

</div>

Emmanuel O. 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
