# DRYing up helper

**URL:** <https://www.ruby-forum.com/t/drying-up-helper/181749>\
**Category:** Rails\
**Created:** [January 17, 2010, 7:50pm UTC](https://www.ruby-forum.com/t/drying-up-helper/181749 "2010-01-17T19:50:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [January 17, 2010, 7:50pm UTC](https://www.ruby-forum.com/t/drying-up-helper/181749/1 "2010-01-17T19:50:44Z")

</div>

Hi,

I’m trying to make a helper in my application DRYer. Here is an edited  
version of the current relevant code:

module MyControllerHelper  
PERSON\_FIELDS = [‘last\_name’, ‘first\_name’]  
ADDRESS\_FIELDS = [‘address\_1’, ‘address\_2’, ‘city’, ‘state’, ‘zip’]

def get\_errors\_on(section)  
errors = ‘’

```
@person.errors.each do |attr,msg|
  case section
  when 'person'
    errors += "#{attr.humanize} #{msg}<br>" if

```

PERSON\_FIELDS.include? attr  
when ‘address’  
errors += “#{attr.humanize} #{msg}  
” if  
ADDRESS\_FIELDS.include? attr  
end  
end

```
errors

```

end # def get\_errors\_on(section)  
end # module WizardHelper

I have tried different versions of the following but always  
unsuccessfully:  
@person.errors.each do |attr,msg|  
errors += “#{attr.humanize} #{msg}  
” if (section.upcase +  
‘\_FIELDS’).constantize.include? attr  
end

I always get a message saying that, for example, PERSON\_FIELDS  
constant has not been defined, although it is defined at the top of  
the module.

Any ideas?

Thank you.

---

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [January 17, 2010, 11:36pm UTC](https://www.ruby-forum.com/t/drying-up-helper/181749/2 "2010-01-17T23:36:38Z")

</div>

pepe, you should be able to use eval(section.upcase + ‘\_FIELDS’)  
instead of constantize.

That being said, I can never suggest using eval unless I accompany  
that with a warning, a la  
[Railsrocket.com is for sale | HugeDomains](http://www.railsrocket.com/articles/the-controversial-eval-function).  
Also, I always valued readability over being DRY. Being DRY in your  
case might be useful if you can reasonably expect to have more than  
just PERSON and ADDRESS fields. Otherwise, you might be sacrificing  
readability without cause.

Liam

---

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [January 18, 2010, 4:14am UTC](https://www.ruby-forum.com/t/drying-up-helper/181749/3 "2010-01-18T04:14:07Z")

</div>

Hi Liam,

I tried to use eval but couldn’t make it work in my prior attempts.  
I’ll give it a shot again.

About the readability of the code I agree with you, my priority is  
always make my code easy to read, then make it DRY. I currently have a  
total of 4 sections that I have to run this code for but based on what  
I know I very possibly will need to run the code for many more  
sections in the future. If I get this one working right it will be a  
big help in the future, but when the code gets ‘obscure’ I always  
leave extensive comments for my sanity and that of the person coming  
behind.

Thanks a lot.

---

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [January 18, 2010, 4:35am UTC](https://www.ruby-forum.com/t/drying-up-helper/181749/4 "2010-01-18T04:35:47Z")

</div>

eval worked indeed.

Thanks again. The solution, as usual, was simpler than what I thought  
it would be. 🙂
