# How to Change Up Methods According to Parameter

**URL:** <https://www.ruby-forum.com/t/how-to-change-up-methods-according-to-parameter/161755>\
**Category:** Rails\
**Created:** [March 2, 2009, 5:07am UTC](https://www.ruby-forum.com/t/how-to-change-up-methods-according-to-parameter/161755 "2009-03-02T05:07:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [March 2, 2009, 5:07am UTC](https://www.ruby-forum.com/t/how-to-change-up-methods-according-to-parameter/161755/1 "2009-03-02T05:07:24Z")

</div>

Hi…  
I have two methods.but the parameter will be changing.  
For the first method i send only one parameter(f).  
But for the second method i’ll send 3  
parameters(f,search\_string\_1,search\_string\_2).  
And also for 3rd Method i send Two Parameter(f,search\_string).

Is it possible to optimise below methods into Single Methods .  
How can i combine the below Three Methods into one method pls help me.

#method 1  
def check\_chat\_type(f)  
Dir.foreach(File.join(RAILS\_ROOT +  
“/public/ACMServer\_exe/ChatHistory”,f)) do sub\_folder\_name|  
if sub\_folder\_name.include? “Vs”  
get\_path(f,sub\_folder\_name)  
end  
end  
end

#method 2  
def check\_chat\_type(f,search\_string\_1,search\_string\_2)  
Dir.foreach(File.join(RAILS\_ROOT +  
“/public/ACMServer\_exe/ChatHistory”,f)) do  
|sub\_folder\_name|  
if sub\_folder\_name.include? “Vs”  
if sub\_folder\_name.include?(search\_string\_1)  
if sub\_folder\_name.include?(search\_string\_2)  
get\_path(f,sub\_folder\_name)  
end  
end  
end  
end  
end

#Method 3  
def check\_chat\_type(f,search\_string)  
Dir.foreach(File.join(RAILS\_ROOT +  
“/public/ACMServer\_exe/ChatHistory”,f)) do  
|sub\_folder\_name|  
if sub\_folder\_name.include? “Vs”  
if sub\_folder\_name.include?(search\_string)  
get\_path(f,sub\_folder\_name)  
end  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [March 2, 2009, 10:30am UTC](https://www.ruby-forum.com/t/how-to-change-up-methods-according-to-parameter/161755/2 "2009-03-02T10:30:33Z")

</div>

# this makes param 2 and 3 optional (if none is passed default values

are set to empty string):  
def check\_chat\_type(f, search\_string\_1="", search\_string\_2="")  
…
