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
Post a Comment