# With or without arguments - how?

**URL:** <https://www.ruby-forum.com/t/with-or-without-arguments-how/158520>\
**Category:** Ruby\
**Created:** [January 24, 2009, 7:03am UTC](https://www.ruby-forum.com/t/with-or-without-arguments-how/158520 "2009-01-24T07:03:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [January 24, 2009, 7:03am UTC](https://www.ruby-forum.com/t/with-or-without-arguments-how/158520/1 "2009-01-24T07:03:14Z")

</div>

I’ve used methods before that work with or without arguments. I’ve tried  
to figure it out but can’t get to the bottom of it. Could someone give  
me a hint how you would go about creating a method that works for either  
of these cases?

mymethod(x, y)  
x + y  
end

Then somehow get this to work without the (x, y) arguments…? What I  
want is to have a method that can do something on default if no  
arguments are spec’d or I can refine what it does by adding arguments.

---

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [January 24, 2009, 7:20am UTC](https://www.ruby-forum.com/t/with-or-without-arguments-how/158520/2 "2009-01-24T07:20:29Z")

</div>

On Jan 23, 11:01 pm, Jason L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ## want is to have a method that can do something on default if no arguments are spec’d or I can refine what it does by adding arguments.
> 
> Posted viahttp://www.ruby-forum.com/.

What methods? Have you looked at the source for those methods? Have  
you read Programming Ruby? What about this section (http://www.ruby-  
[doc.org/docs/ProgrammingRuby/html/tut\_methods.html](http://doc.org/docs/ProgrammingRuby/html/tut_methods.html))?

HTH,  
Chris

---

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [January 24, 2009, 7:21am UTC](https://www.ruby-forum.com/t/with-or-without-arguments-how/158520/3 "2009-01-24T07:21:40Z")

</div>

Try this:  
my\_method(x = 1, y = 1)  
x + y  
end

Warmest Regards,

Victor H. Goff III

On Fri, Jan 23, 2009 at 11:01 PM, Jason L. \<

---

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [January 24, 2009, 7:24am UTC](https://www.ruby-forum.com/t/with-or-without-arguments-how/158520/4 "2009-01-24T07:24:43Z")

</div>

OK, sorry this was a dumb question. I missed default values.

Thank you.

---

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [January 24, 2009, 5:41pm UTC](https://www.ruby-forum.com/t/with-or-without-arguments-how/158520/5 "2009-01-24T17:41:24Z")

</div>

On 24.01.2009 07:22, Jason L. wrote:

> OK, sorry this was a dumb question. I missed default values.

… and probably the splash operator as well which can be used to  
similar purpose.

Kind regards

robert
