# How to DRY This? (params=\>var?)

**URL:** <https://www.ruby-forum.com/t/how-to-dry-this-params-var/91217>\
**Category:** Rails\
**Created:** [April 5, 2007, 6:40pm UTC](https://www.ruby-forum.com/t/how-to-dry-this-params-var/91217 "2007-04-05T18:40:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sneptune](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sneptune](https://www.ruby-forum.com/u/sneptune)\
**Post date:** [April 5, 2007, 6:40pm UTC](https://www.ruby-forum.com/t/how-to-dry-this-params-var/91217/1 "2007-04-05T18:40:14Z")

</div>

Hi everyone,

Is it possible to dry this code?

recipients = params[:mail][:recipients]  
from = params[:mail][:from]  
subject = params[:mail][:subject]  
email\_body = params[:mail][:email\_body]  
user = params[:mail][:user]

---

<div class="post-metadata">

**Author:** ![sneptune](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sneptune](https://www.ruby-forum.com/u/sneptune)\
**Post date:** [April 5, 2007, 7:07pm UTC](https://www.ruby-forum.com/t/how-to-dry-this-params-var/91217/2 "2007-04-05T19:07:14Z")

</div>

It probably depends on what you’re doing with it.  
If this is going into a model, models accept a hash as an argument to  
new  
(or to update\_attributes, for that matter) such that you could do  
Model.new(params[:mail]),  
and it would auto-populate the fields.  
If it’s not, and you just need all those variables to pass as arguments  
to  
something else, then you probably don’t need to pull them out into local  
variables.  
But, if you were absolutely 100% needing to DRY that code up, you could  
do  
something similar to this:  
class MailInfo  
def initialize(options = {})  
options.each do |key, val|  
self.instance\_variable\_set("@#{key}", val)  
self.class.class\_eval “attr\_accessor :#{key}”  
end  
end  
end  
Then do  
m = MailInfo.new(params[:mail])  
and you should be able to do  
m.email\_body  
m.recipients  
etc.

---

<div class="post-metadata">

**Author:** ![sneptune](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sneptune](https://www.ruby-forum.com/u/sneptune)\
**Post date:** [April 5, 2007, 7:13pm UTC](https://www.ruby-forum.com/t/how-to-dry-this-params-var/91217/3 "2007-04-05T19:13:14Z")

</div>

On Apr 5, 6:40 pm, Wai T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi everyone,
> 
> Is it possible to dry this code?
> 
> recipients = params[:mail][:recipients]  
> from = params[:mail][:from]  
> subject = params[:mail][:subject]  
> email\_body = params[:mail][:email\_body]  
> user = params[:mail][:user]

mail = params[:mail]  
recipients = mail[:from]  
subject = mail[:subject]  
…

That saves you some typing.

But if you want to put your hash variables in local variables, then  
you can’t. Well, actually, you can, by using eval. But that’s very,  
VERY evil and will open the infinite floodgate of security problems.

---

<div class="post-metadata">

**Author:** ![sneptune](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sneptune](https://www.ruby-forum.com/u/sneptune)\
**Post date:** [April 8, 2007, 1:07pm UTC](https://www.ruby-forum.com/t/how-to-dry-this-params-var/91217/4 "2007-04-08T13:07:14Z")

</div>

Luke,

thanks for that snippet. I find that seeing these little titbits of  
code from time to time really helps. They become seeds that later  
spring to life when you want to do something and dont quite know how.  
The problem I find is either remembering these things, or knowing how  
to put them somewhere that I can remember to find them. Thank  
goodness for Google.

tonypm

---

<div class="post-metadata">

**Author:** ![sneptune](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sneptune](https://www.ruby-forum.com/u/sneptune)\
**Post date:** [April 5, 2007, 7:27pm UTC](https://www.ruby-forum.com/t/how-to-dry-this-params-var/91217/5 "2007-04-05T19:27:09Z")

</div>

I see. Thanks Hongli L…
