# Methods optional arguments

**URL:** <https://www.ruby-forum.com/t/methods-optional-arguments/171246>\
**Category:** Ruby\
**Created:** [June 29, 2009, 2:20pm UTC](https://www.ruby-forum.com/t/methods-optional-arguments/171246 "2009-06-29T14:20:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![txapelgorri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5f8ce5/32.png) [@txapelgorri](https://www.ruby-forum.com/u/txapelgorri)\
**Post date:** [June 29, 2009, 2:20pm UTC](https://www.ruby-forum.com/t/methods-optional-arguments/171246/1 "2009-06-29T14:20:20Z")

</div>

Hi there:

I was learning about methods, and when I was trying to use optional  
arguments I get stucked with this:

def my\_method (a=‘This’, b=‘is’, c=‘great’)  
puts a+’ ‘+b+’ '+c  
end

If I try:  
my\_method(‘Ruby’)  
-\> Ruby is great  
-\> nil

But if I try:  
my\_method(,‘cool’)  
-\> SyntaxError: compile error

Any idea on how to use arguments different from the first one?.

Thank’s in advance, Ibon.

---

<div class="post-metadata">

**Author:** ![txapelgorri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5f8ce5/32.png) [@txapelgorri](https://www.ruby-forum.com/u/txapelgorri)\
**Post date:** [June 29, 2009, 3:59pm UTC](https://www.ruby-forum.com/t/methods-optional-arguments/171246/2 "2009-06-29T15:59:45Z")

</div>

On Mon, Jun 29, 2009 at 8:20 AM, Ibon Castilla[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ## my\_method(‘Ruby’)
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Optional arguments must be passed in the order they’re declared, and  
cannot be omitted. Instead, you’ll often see hashes used instead.

def my\_method(options={})  
o = {  
:a =\> ‘This’,  
:b =\> ‘is’,  
:c =\> ‘cool’  
}.merge(options)

puts “#{o[:a]} #{o[:b]} #{o[:c]}”  
end

my\_method( :c =\> ‘great’ )

---

<div class="post-metadata">

**Author:** ![txapelgorri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5f8ce5/32.png) [@txapelgorri](https://www.ruby-forum.com/u/txapelgorri)\
**Post date:** [June 29, 2009, 5:06pm UTC](https://www.ruby-forum.com/t/methods-optional-arguments/171246/3 "2009-06-29T17:06:37Z")

</div>

Michael M. wrote:

> On Mon, Jun 29, 2009 at 8:20 AM, Ibon Castilla[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > ## ï¿½my\_method(‘Ruby’)
> > 
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> Optional arguments must be passed in the order they’re declared, and  
> cannot be omitted. Instead, you’ll often see hashes used instead.
> 
> def my\_method(options={})  
> o = {  
> :a =\> ‘This’,  
> :b =\> ‘is’,  
> :c =\> ‘cool’  
> }.merge(options)
> 
> puts “#{o[:a]} #{o[:b]} #{o[:c]}”  
> end
> 
> my\_method( :c =\> ‘great’ )

I agree that’s probably the best approach in most cases. If you really  
want ‘optional’ earlier arguments, you’d have to do something like this:

def my\_method(a=nil, b=nil, c=nil)  
a ||= ‘This’  
b ||= ‘is’  
c ||= ‘great’  
puts a+’ ‘+b+’ '+c  
end

p my\_method(nil, nil, ‘cool’)

---

<div class="post-metadata">

**Author:** ![txapelgorri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5f8ce5/32.png) [@txapelgorri](https://www.ruby-forum.com/u/txapelgorri)\
**Post date:** [June 29, 2009, 5:34pm UTC](https://www.ruby-forum.com/t/methods-optional-arguments/171246/4 "2009-06-29T17:34:22Z")

</div>

Hi both:

Thanks for the code. I’ll print it out to keep it safe, and not to hold  
on my memory 😉

Cheers, Ibon.
