Method constant arguments

  • Thread starter Alexandre Rosenfeld
  • Start date
A

Alexandre Rosenfeld

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
 
P

Phrogz

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?

Here's one way:

def dosomehomework( directory, options={} )
if options[:eek:verwrite] then
...
end

dosomehomework '/', :use_id_as_filename=>true, :eek:verwrite=>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.
 
R

Robert Klemme

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?

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('/', :eek:verwrite => true, :use_id_as_filename => true)

Kind regards

robert
 
A

Alex Young

Alexandre said:
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?

I've seen code that does this:

def dosomework(directory, *opts)
if opts.include? :eek:verwrite
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.
 
T

Timothy Hunter

Alexandre said:
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
Simple solution - just use constants

Overwrite = true
UseIdAsFilename = false
 
R

Robert Klemme

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
 
A

Alexandre Rosenfeld

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 said:
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[:eek:verwrite] then
...
end

dosomehomework '/', :use_id_as_filename=>true, :eek:verwrite=>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.
 
R

Rick DeNatale

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 Kistner wrote:
Here's one way:

def dosomehomework( directory, options={} )
if options[:eek:verwrite] 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 = {
:eek:verwrite => true,
:eek:theroption => :default_value
}.merge(options)
...
}

Instead of:

def dosomehomework(directory, options={ :eek:verwrite => true,
:eek:theroption => :default_value })
...

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top