我有一個心智塊,我有一段代碼,我想保留在一個方法中,但以一種乾淨的方式。以更清晰的方式重寫代碼
這是這裏的想法;
def self.number_price(user_id,to)
user = User.find(user_id)
user_prices = user.prices
user_prices.each do |price|
if to =~ /^(#{price.prefix})/
return price.send("price_#{user.currency.downcase}").to_f
end
end
end
但是,如果下面的循環不返回任何東西,我想返回這個;
return DefaultPrices.send("price_#{user_currency.downcase}").to_f
否則,該方法將默認返回user_prices = user.prices
有人結果表明這樣的一些乾淨的方式.. 謝謝
如果您幫助我們理解代碼在更高級別上應該做什麼,例如通過至少解釋預期的輸入和輸出,您會更成功地獲得答案。 –
您可以使用http://www.ruby-doc.org/core-2.1.0/Enumerable.html#method-i-find – akonsu
所有已經回答的人似乎都知道對象DefaultPrices是什麼。我不。請解釋。 –