Forum: Ruby on Rails How to Change Up Methods According to Parameter

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3083ca8f0496edaac2fa10c20630d0bc?d=identicon&s=25 Newb Newb (newb)
on 2009-03-02 05:07
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
80e4cb97cae5c8d745f72337d93fd8f2?d=identicon&s=25 MaD (Guest)
on 2009-03-02 10:30
(Received via mailing list)
# 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="")
...
This topic is locked and can not be replied to.