# How to refactor the following 2 function (4 lines each)

**URL:** <https://www.ruby-forum.com/t/how-to-refactor-the-following-2-function-4-lines-each/174493>\
**Category:** Rails\
**Created:** [August 27, 2009, 3:41pm UTC](https://www.ruby-forum.com/t/how-to-refactor-the-following-2-function-4-lines-each/174493 "2009-08-27T15:41:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Axinte\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/43a26b/32.png) [@Axinte\_L](https://www.ruby-forum.com/u/Axinte_L)\
**Post date:** [August 27, 2009, 3:41pm UTC](https://www.ruby-forum.com/t/how-to-refactor-the-following-2-function-4-lines-each/174493/1 "2009-08-27T15:41:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Axinte\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/43a26b/32.png) [@Axinte\_L](https://www.ruby-forum.com/u/Axinte_L)\
**Post date:** [August 27, 2009, 4:00pm UTC](https://www.ruby-forum.com/t/how-to-refactor-the-following-2-function-4-lines-each/174493/2 "2009-08-27T16:00:27Z")

</div>

def normalize(property)  
result = normalize\_generic(self.send(property))  
self.send(“#{property}\_min=”, result[:min])  
self.send(“#{property}\_max=”, result[:max])  
self.send(“#{property}=”, result[:display])  
end

object.normalize(:size)  
object.normalize(:budget)

- 

Maurício Linhares  
[http://codeshooter.wordpress.com/](http://codeshooter.wordpress.com/) | [http://twitter.com/mauriciojr](http://twitter.com/mauriciojr)

---

<div class="post-metadata">

**Author:** ![Axinte\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/43a26b/32.png) [@Axinte\_L](https://www.ruby-forum.com/u/Axinte_L)\
**Post date:** [August 27, 2009, 4:12pm UTC](https://www.ruby-forum.com/t/how-to-refactor-the-following-2-function-4-lines-each/174493/3 "2009-08-27T16:12:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Axinte\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/43a26b/32.png) [@Axinte\_L](https://www.ruby-forum.com/u/Axinte_L)\
**Post date:** [August 27, 2009, 4:37pm UTC](https://www.ruby-forum.com/t/how-to-refactor-the-following-2-function-4-lines-each/174493/4 "2009-08-27T16:37:20Z")

</div>

Thank you both for your feedback. Based on your suggestions, I’ve  
finally  
decided to implement it in the following way:

%w[size budget].each do |property|  
define\_method(“normalize\_#{property}\_range”) do |\*args|  
result = normalize\_generic\_range(self.send(property))  
self.send(“#{property}\_min=”, result[:min])  
self.send(“#{property}\_max=”, result[:max])  
self.send(“#{property}=”, result[:display])  
end  
end

this code defines normalize\_size\_range and normalize\_budget\_range and  
updates both sets of attributes accordingly.

kind regards,  
Axinte

2009/8/27 Gianluca T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Axinte\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/43a26b/32.png) [@Axinte\_L](https://www.ruby-forum.com/u/Axinte_L)\
**Post date:** [August 27, 2009, 5:17pm UTC](https://www.ruby-forum.com/t/how-to-refactor-the-following-2-function-4-lines-each/174493/5 "2009-08-27T17:17:43Z")

</div>

Here’s a better solution:

%w[size budget].each do |property|  
class\_eval %Q!  
def normalize\_#{property}\_range  
result = normalize\_generic\_range( self.#{property} )  
self.#{property}\_min = result[:min]  
self.#{property}\_max = result[:max]  
self.#{property} = result[:display]  
end  
!  
end

- 

Maurício Linhares  
[http://codeshooter.wordpress.com/](http://codeshooter.wordpress.com/) | [http://twitter.com/mauriciojr](http://twitter.com/mauriciojr)

---

<div class="post-metadata">

**Author:** ![Axinte\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/43a26b/32.png) [@Axinte\_L](https://www.ruby-forum.com/u/Axinte_L)\
**Post date:** [August 27, 2009, 10:26pm UTC](https://www.ruby-forum.com/t/how-to-refactor-the-following-2-function-4-lines-each/174493/6 "2009-08-27T22:26:49Z")

</div>

Mauricio,

Thank you again for your help. Is there any difference between your  
solution  
and mine?  
Kind regards,  
Axinte

2009/8/27 MaurÃ­cio Linhares [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Axinte\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/43a26b/32.png) [@Axinte\_L](https://www.ruby-forum.com/u/Axinte_L)\
**Post date:** [August 28, 2009, 12:05am UTC](https://www.ruby-forum.com/t/how-to-refactor-the-following-2-function-4-lines-each/174493/7 "2009-08-28T00:05:10Z")

</div>

Yes, you’re defining and method and using send at every call, my  
solution will generate a method with direct calls instead of sending  
them.

- 

Maurício Linhares  
[http://codeshooter.wordpress.com/](http://codeshooter.wordpress.com/) | [http://twitter.com/mauriciojr](http://twitter.com/mauriciojr)
