# Default method parameters

**URL:** <https://www.ruby-forum.com/t/default-method-parameters/115588>\
**Category:** Rails\
**Created:** [September 26, 2007, 5:17pm UTC](https://www.ruby-forum.com/t/default-method-parameters/115588 "2007-09-26T17:17:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cassiano\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ba9def/32.png) [@Cassiano\_R](https://www.ruby-forum.com/u/Cassiano_R)\
**Post date:** [September 26, 2007, 5:17pm UTC](https://www.ruby-forum.com/t/default-method-parameters/115588/1 "2007-09-26T17:17:25Z")

</div>

Hello

I have an method like this:  
def test(x, y=’’, z=’’)  
puts “x=#{x} y=#{y} z=#{z}”  
end

and I want call with x and z parameters:  
test(‘x’, z=‘z’)  
output:  
x=‘x’ y=‘z’ z=’’

How you can see, the Z value appears in Y parameter…  
and I want call just with X and Z parameters

Thanks a lot!

Cassiano R.  
RS - Brazil

---

<div class="post-metadata">

**Author:** ![Cassiano\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ba9def/32.png) [@Cassiano\_R](https://www.ruby-forum.com/u/Cassiano_R)\
**Post date:** [September 26, 2007, 5:39pm UTC](https://www.ruby-forum.com/t/default-method-parameters/115588/2 "2007-09-26T17:39:24Z")

</div>

This is a ruby question rather than a rails question.  
Anyway, ruby doesn’t have named parameters, you you just can’t do that.  
You’re passing z=‘z’ as the second argument, which evaluates to ‘z’ and  
so y is set to ‘z’.  
With ruby’s default arguments stuff you must pass in the leftmost n  
parameters, for some value of n. (ie no ‘gaps’)

Fred

---

<div class="post-metadata">

**Author:** ![Cassiano\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ba9def/32.png) [@Cassiano\_R](https://www.ruby-forum.com/u/Cassiano_R)\
**Post date:** [September 26, 2007, 7:00pm UTC](https://www.ruby-forum.com/t/default-method-parameters/115588/3 "2007-09-26T19:00:46Z")

</div>

You can always simulate it by passing in a hash:

def test(in\_hsh={})  
puts “x=#{in\_hsh[‘x’]} y=#{in\_hsh[‘y’]} z=#{in\_hsh[‘z’]}”  
end

test({‘x’ =\> ‘x’, ‘z’ =\> ‘z’)

Cassiano R. wrote:

> x=‘x’ y=‘z’ z=’’
> 
> >

–  
Sincerely,

William P.
