ruby - How to clean up this very simple Rails function? -


is there way pretty rails code?

def function   if new_record?                           thing   else     thing + yet_another_thing   end end 

i don't repetition of thing here, wonder if there's cleaner way.

thanks help.

this works objects support +, (even strings.)

 [thing, (yet_another_thing unless new_record?)].compact.inject(:+) 

it's dry , scary, being trapped in desert without water.


you might able away with:

 thing.dup.tap{|t| t << yet_another_thing unless new_record?} 

this won't work if thing integer (you can't dup it) , needs support << operator.

also dry scary in different way.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -