Hi,
try this:
def normalize
update_result("budget")
update_result("size")
end
def update_result(field_name)
result = normalize_generic(self.send(field_name))
eval("self.#{field_name}_min = result[:min]")
eval("self.#{field_name}_max = result[:max]")
eval("self.#{field_name} = result[:display]")
end
> Hello
>
> I'm trying to DRY my code and I think It could be done even better.
> The following 2 functions are "before_save" callbacks that practically
> do the same thing, but on a different set of attributes.
>
> Since the code is highly similar, I was wondering if someone could
> help me to refactor the code?
>
>
> def normalize_budget
> result = normalize_generic(self.budget)
> self.budget_min = result[:min]
> self.budget_max = result[:max]
> self.budget = result[:display]
> end
>
> def normalize_size
> result = normalize_generic(self.size)
> self.size_min = result[:min]
> self.size_max = result[:max]
> self.size = result[:display]
> end
>
>
> Kind regards,
> Axinte
>
> >
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---