Method constant arguments

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

On Apr 6, 11:47 pm, Alexandre R. [email protected] 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.

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

Alexandre R. wrote:

Simple solution - just use constants

Overwrite = true
UseIdAsFilename = false

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

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.

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 :wink: 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. [email protected] 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.

On 4/7/07, Alexandre R. [email protected] 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/