[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Question on optional method parameters

aidy

6/4/2006 6:42:00 PM

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

5 Answers

joesb.coe9

6/5/2006 2:40:00 AM

0


aidy wrote:
> 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)

> Or should I be using setters?
>
> Thanks
>
> Aidy

Trans

6/5/2006 3:35:00 AM

0


aidy wrote:
> 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.

Trans

6/5/2006 3:36:00 AM

0


Trans wrote:
> 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.

joesb.coe9

6/5/2006 4:21:00 AM

0


Trans wrote:
> Trans wrote:
> > 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)

?

Trans

6/5/2006 4:34:00 AM

0


joesb wrote:
> Trans wrote:
> > Trans wrote:
> > > 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.