How can I refactor ruby code that calls different functions but does the same thing with their responses? -


this method refactor. way refactor code? there way put method calls in list , return 1 of method returns valid response?

  def method     response_hash = method1     return response_hash if response_hash.present?      response_hash = method2     return response_hash if response_hash.present?      response_hash = method3     return response_hash if response_hash.present?      response_hash = method4     return response_hash if response_hash.present?   end 

seems want return first non-empty result.

def my_method   [:method1, :method2, :method3, :method4].each |method_name|     result = send(method_name)     return result if result.present?   end end 

symbols / send here maintain lazy nature of evaluations (don't evaluate more necessary)


Comments

Popular posts from this blog

searchKeyword not working in AngularJS filter -

sequelize.js - Sequelize: sort by enum cases -

user interface - how to replace an ongoing process of image capture from another process call over the same ImageLabel in python's GUI TKinter -