[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

string as optional parameter for method?

Varun Goel

5/14/2008 3:34:00 PM

Hi All,
I have one method like def hello(int,string)
Now i want that if user pass int value or string value then it should
search on the basis of one of those.

means i want both as optional parameter.
Please tell me how to do this in ROR.
Thanks
Varun
--
Posted via http://www.ruby-....

1 Answer

Axel Etzold

5/14/2008 3:51:00 PM

0


-------- Original-Nachricht --------
> Datum: Thu, 15 May 2008 00:33:37 +0900
> Von: Varun Goel <varun.rajeshkumar@gmail.com>
> An: ruby-talk@ruby-lang.org
> Betreff: string as optional parameter for method?

> Hi All,
> I have one method like def hello(int,string)
> Now i want that if user pass int value or string value then it should
> search on the basis of one of those.
>
> means i want both as optional parameter.
> Please tell me how to do this in ROR.
> Thanks
> Varun
> --
> Posted via http://www.ruby-....

Dear Varun,

maybe something like this ?


def hello(first=nil,second=nil)
if first.class==Fixnum and second.class==String
p 'you entered something for both int and string'
elsif first.class==Fixnum
p 'you entered something for int'
elsif first.class==String and second==nil
p 'you entered something for string'
else
p 'come on, read the manual!'
end
end


p hello(5,'string')
p hello(5)
p hello('string')
p hello(5.78,nil)
p hello()


Best regards,

Axel
--
GMX startet ShortView.de. Hier findest Du Leute mit Deinen Interessen!
Jetzt dabei sein: http://www.shortview.de/?mc=sv_...