# Method constant arguments

**URL:** <https://www.ruby-forum.com/t/method-constant-arguments/91351>\
**Category:** Ruby\
**Created:** [April 7, 2007, 7:47am UTC](https://www.ruby-forum.com/t/method-constant-arguments/91351 "2007-04-07T07:47:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![airmind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ecd19e/32.png) [@airmind](https://www.ruby-forum.com/u/airmind)\
**Post date:** [April 7, 2007, 7:47am UTC](https://www.ruby-forum.com/t/method-constant-arguments/91351/1 "2007-04-07T07:47:53Z")

</div>

Hi, I’m new to ruby, and I’m loving it so far.

I want to call a method with some parameters that would indicate a  
state, so most of them would be only a true or false value, however  
that’s not very good to read when calling the method. Like this:

def dosomework(directory, overwrite = false, use\_id\_as\_filename =  
true)

dosomework(’/’, true, false)

I would like to call that function with some readable parameters,  
something like this:

dosomework(’/’, Overwrite, UseIdAsFilename)

I have tried array arguments and hashes, but I didn’t like those  
solutions. What would be the recommended way to do that in Ruby?

Thanks

---

<div class="post-metadata">

**Author:** ![airmind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ecd19e/32.png) [@airmind](https://www.ruby-forum.com/u/airmind)\
**Post date:** [April 7, 2007, 8:55am UTC](https://www.ruby-forum.com/t/method-constant-arguments/91351/2 "2007-04-07T08:55:59Z")

</div>

On Apr 6, 11:47 pm, Alexandre R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have tried array arguments and hashes, but I didn’t like those  
> solutions. What would be the recommended way to do that in Ruby?

Here’s one way:

def dosomehomework( directory, options={} )  
if options[:overwrite] then  
…  
end

dosomehomework ‘/’, :use\_id\_as\_filename=\>true, :overwrite=\>false

You lose the self-documentating nature of the method arguments, but  
gain clarity in the method call as well as order-independance.

Rumor has it Ruby 2.0 will have keyword arguments; I’m not sure what  
the current proposal is, but iirc you’ll have the best of both worlds  
there.

---

<div class="post-metadata">

**Author:** ![airmind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ecd19e/32.png) [@airmind](https://www.ruby-forum.com/u/airmind)\
**Post date:** [April 7, 2007, 9:06am UTC](https://www.ruby-forum.com/t/method-constant-arguments/91351/3 "2007-04-07T09:06:00Z")

</div>

On 07.04.2007 07:47, Alexandre R. wrote:

> I would like to call that function with some readable parameters,  
> something like this:
> 
> dosomework(’/’, Overwrite, UseIdAsFilename)
> 
> I have tried array arguments and hashes, but I didn’t like those  
> solutions. What would be the recommended way to do that in Ruby?

There are tons of ways. You could use bit mapped flags (see File or  
Regexp for example). Or you use a Hash like this

do\_some\_work(’/’, :overwrite =\> true, :use\_id\_as\_filename =\> true)

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![airmind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ecd19e/32.png) [@airmind](https://www.ruby-forum.com/u/airmind)\
**Post date:** [April 7, 2007, 2:49pm UTC](https://www.ruby-forum.com/t/method-constant-arguments/91351/4 "2007-04-07T14:49:38Z")

</div>

Alexandre R. wrote:

> 

Simple solution - just use constants

Overwrite = true  
UseIdAsFilename = false

---

<div class="post-metadata">

**Author:** ![airmind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ecd19e/32.png) [@airmind](https://www.ruby-forum.com/u/airmind)\
**Post date:** [April 7, 2007, 3:00pm UTC](https://www.ruby-forum.com/t/method-constant-arguments/91351/5 "2007-04-07T15:00:39Z")

</div>

On 07.04.2007 14:48, Timothy H. wrote:

> > dosomework(’/’, true, false)
> 
> Simple solution - just use constants
> 
> Overwrite = true  
> UseIdAsFilename = false

IMHO that’s not a good solution as you still have to put them in the  
proper position of the argument list. If you use constants than they  
make more sense when using bit operations (like File).

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![airmind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ecd19e/32.png) [@airmind](https://www.ruby-forum.com/u/airmind)\
**Post date:** [April 7, 2007, 9:07am UTC](https://www.ruby-forum.com/t/method-constant-arguments/91351/6 "2007-04-07T09:07:00Z")

</div>

Alexandre R. wrote:

> I would like to call that function with some readable parameters,  
> something like this:
> 
> dosomework(’/’, Overwrite, UseIdAsFilename)
> 
> I have tried array arguments and hashes, but I didn’t like those  
> solutions. What would be the recommended way to do that in Ruby?

I’ve seen code that does this:

def dosomework(directory, \*opts)  
if opts.include? :overwrite  
dosomething  
end  
if opts.include? :use\_id\_as\_filename  
dosomethingelse  
end  
dofoo(directory)  
end

I don’t know if that’s useful at all… it’d certainly be quite easy to  
write a Struct-like class that you could use in place of the opts array  
if you wanted to tidy it up.

---

<div class="post-metadata">

**Author:** ![airmind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ecd19e/32.png) [@airmind](https://www.ruby-forum.com/u/airmind)\
**Post date:** [April 7, 2007, 9:32pm UTC](https://www.ruby-forum.com/t/method-constant-arguments/91351/7 "2007-04-07T21:32:37Z")

</div>

Thank you all for the answers. I really liked the solution below. It can  
handle a lot of the things I want to do. I can set overwrite to true as  
default for instance.  
I remember keyword arguments when I began learning Python and I liked  
it. Then I found Ruby and I couldnt go back 😉 It would be a nice thing  
to have it in Ruby.

Thanks a lot.

Gavin K. wrote:

> On Apr 6, 11:47 pm, Alexandre R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I have tried array arguments and hashes, but I didn’t like those  
> > solutions. What would be the recommended way to do that in Ruby?
> 
> Here’s one way:
> 
> def dosomehomework( directory, options={} )  
> if options[:overwrite] then  
> …  
> end
> 
> dosomehomework ‘/’, :use\_id\_as\_filename=\>true, :overwrite=\>false
> 
> You lose the self-documentating nature of the method arguments, but  
> gain clarity in the method call as well as order-independance.
> 
> Rumor has it Ruby 2.0 will have keyword arguments; I’m not sure what  
> the current proposal is, but iirc you’ll have the best of both worlds  
> there.

---

<div class="post-metadata">

**Author:** ![airmind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ecd19e/32.png) [@airmind](https://www.ruby-forum.com/u/airmind)\
**Post date:** [April 7, 2007, 10:31pm UTC](https://www.ruby-forum.com/t/method-constant-arguments/91351/8 "2007-04-07T22:31:27Z")

</div>

On 4/7/07, Alexandre R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thank you all for the answers. I really liked the solution below. It can  
> handle a lot of the things I want to do. I can set overwrite to true as  
> default for instance.

> Gavin K. wrote:

> > Here’s one way:
> > 
> > def dosomehomework( directory, options={} )  
> > if options[:overwrite] then  
> > …  
> > end

Keep in mind that if you want to set defaults, you probably want to do  
it something like this:

def dosomehomework(directory, options={})

options = {  
:overwrite =\> true,  
:otheroption =\> :default\_value  
}.merge(options)  
…  
}

Instead of:

def dosomehomework(directory, options={ :overwrite =\> true,  
:otheroption =\> :default\_value })  
…

In the second case, if the user specified any option it would wipe out  
all of the defaults.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)
