# String as optional parameter for method?

**URL:** <https://www.ruby-forum.com/t/string-as-optional-parameter-for-method/137611>\
**Category:** Ruby\
**Created:** [May 14, 2008, 5:33pm UTC](https://www.ruby-forum.com/t/string-as-optional-parameter-for-method/137611 "2008-05-14T17:33:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![varunkumargoel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ed8c4c/32.png) [@varunkumargoel](https://www.ruby-forum.com/u/varunkumargoel)\
**Post date:** [May 14, 2008, 5:33pm UTC](https://www.ruby-forum.com/t/string-as-optional-parameter-for-method/137611/1 "2008-05-14T17:33:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![varunkumargoel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ed8c4c/32.png) [@varunkumargoel](https://www.ruby-forum.com/u/varunkumargoel)\
**Post date:** [May 14, 2008, 5:44pm UTC](https://www.ruby-forum.com/t/string-as-optional-parameter-for-method/137611/2 "2008-05-14T17:44:55Z")

</div>

> -----Original Message-----  
> From: removed\_email\_address@domain.invalid [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]  
> Sent: Wednesday, May 14, 2008 8:34 AM  
> To: ruby-talk ML  
> Subject: 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.

So as far as I understand you want methods overloaded on the parameter  
type? Ruby does not support method overloading directly. One way you can  
do it is something like that:

def hello(arg)  
case arg in  
when String then hello\_with\_string(arg)  
when Integer then hello\_with\_integer(arg)  
else raise “Unsupported parameter type”  
end  
end

Ruby methods may have default parameters, however it may be not what you  
are after as this facility does not guarantee parameter types.

Gennady.

---

<div class="post-metadata">

**Author:** ![varunkumargoel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ed8c4c/32.png) [@varunkumargoel](https://www.ruby-forum.com/u/varunkumargoel)\
**Post date:** [May 14, 2008, 5:53pm UTC](https://www.ruby-forum.com/t/string-as-optional-parameter-for-method/137611/3 "2008-05-14T17:53:13Z")

</div>

-------- Original-Nachricht --------

> Datum: Thu, 15 May 2008 00:33:37 +0900  
> Von: Varun G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> An: removed\_email\_address@domain.invalid  
> Betreff: string as optional parameter for method?

> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).  
> 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
