Question on optional method parameters

A

aidy

Hello,

Sorry to bother you again, but below is a method I have:

def enter_territory(country, depot, *rest)
@ie.select_list( :name, 'criteria.countryId').select(country)
@ie.select_list( :name, 'criteria.depotId').select(depot)
@ie.select_list( :name, 'criteria.lob').select(rest[0]) if rest[0] !=
nil
@ie.select_list( :name, 'criteria.territoryType').select(rest[1]) if
rest[1] != nil
@ie.select_list( :name, 'criteria.viewType').select(rest[2]) if
rest[2] != nil
end

The problem I am having is that sometimes I don't want to pass anything
to the first element of the argument array, but I would like to pass
something to the second element. How would I do this in Ruby?

Or should I be using setters?

Thanks

Aidy
 
J

joesb

aidy said:
Hello,

Sorry to bother you again, but below is a method I have:

def enter_territory(country, depot, *rest)
@ie.select_list( :name, 'criteria.countryId').select(country)
@ie.select_list( :name, 'criteria.depotId').select(depot)
@ie.select_list( :name, 'criteria.lob').select(rest[0]) if rest[0] !=
nil
@ie.select_list( :name, 'criteria.territoryType').select(rest[1]) if
rest[1] != nil
@ie.select_list( :name, 'criteria.viewType').select(rest[2]) if
rest[2] != nil
end

The problem I am having is that sometimes I don't want to pass anything
to the first element of the argument array, but I would like to pass
something to the second element. How would I do this in Ruby?

Pass nil to it?

enter_territory('USA', 1, nil, 2, 3)
 
T

Trans

aidy said:
Hello,

Sorry to bother you again, but below is a method I have:

def enter_territory(country, depot, *rest)
@ie.select_list( :name, 'criteria.countryId').select(country)
@ie.select_list( :name, 'criteria.depotId').select(depot)
@ie.select_list( :name, 'criteria.lob').select(rest[0]) if rest[0] !=
nil
@ie.select_list( :name, 'criteria.territoryType').select(rest[1]) if
rest[1] != nil
@ie.select_list( :name, 'criteria.viewType').select(rest[2]) if
rest[2] != nil
end

The problem I am having is that sometimes I don't want to pass anything
to the first element of the argument array, but I would like to pass
something to the second element. How would I do this in Ruby?
From the looks of it you might be better off using named parameters:

def enter_territory( country, depot, rest )
@ie.select_list( :name, 'criteria.countryId').select(country)
@ie.select_list( :name, 'criteria.depotId').select(depot)
@ie.select_list( :name, 'criteria.lob').select(rest[:lob]) if
rest[:lob]
@ie.select_list( :name,
'criteria.territoryType').select(rest[:territory]) if rest[:territory]
@ie.select_list( :name, 'criteria.viewType').select(rest[2]) if
rest[:view]
end

T.
 
T

Trans

Trans said:
def enter_territory( country, depot, rest )
@ie.select_list( :name, 'criteria.countryId').select(country)
@ie.select_list( :name, 'criteria.depotId').select(depot)
@ie.select_list( :name, 'criteria.lob').select(rest[:lob]) if
rest[:lob]
@ie.select_list( :name,
'criteria.territoryType').select(rest[:territory]) if rest[:territory]
@ie.select_list( :name, 'criteria.viewType').select(rest[2]) if
rest[:view]
end

Sorry, rest[2] should be rest[:view] as well.

T.
 
J

joesb

Trans said:
Trans said:
def enter_territory( country, depot, rest )
@ie.select_list( :name, 'criteria.countryId').select(country)
@ie.select_list( :name, 'criteria.depotId').select(depot)
@ie.select_list( :name, 'criteria.lob').select(rest[:lob]) if
rest[:lob]
@ie.select_list( :name,
'criteria.territoryType').select(rest[:territory]) if rest[:territory]
@ie.select_list( :name, 'criteria.viewType').select(rest[2]) if
rest[:view]
end

Sorry, rest[2] should be rest[:view] as well.

T.

How does this work? Could you please explain?

Am I right to assume that now you have changed rest to become hash
table and change the calling syntax to:

enter_territory('USA', 1, :lob => 2, :view => 3)

?
 
T

Trans

joesb said:
Trans said:
Trans said:
def enter_territory( country, depot, rest )
@ie.select_list( :name, 'criteria.countryId').select(country)
@ie.select_list( :name, 'criteria.depotId').select(depot)
@ie.select_list( :name, 'criteria.lob').select(rest[:lob]) if
rest[:lob]
@ie.select_list( :name,
'criteria.territoryType').select(rest[:territory]) if rest[:territory]
@ie.select_list( :name, 'criteria.viewType').select(rest[2]) if
rest[:view]
end

Sorry, rest[2] should be rest[:view] as well.

T.

How does this work? Could you please explain?

Am I right to assume that now you have changed rest to become hash
table and change the calling syntax to:

enter_territory('USA', 1, :lob => 2, :view => 3)

That's right. The first two parameter are required still, but the rest
can be in any order.

enter_territory('USA', 1, :view => 3, :lob => 2)

(What do the numbers here stand for BTW, or are they just examples?)

Oops, they should also be optional, right? So change the interface
slightly to:

def enter_territory( country, depot, rest={} )

To get a butter understanding try this:

def enter_territory( country, depot, rest={} )
p country
p depot
p rest
end

And then just try a bunch of differnt calls.

HTH,
T.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top