# Variable inside of another variable

**URL:** <https://www.ruby-forum.com/t/variable-inside-of-another-variable/120534>\
**Category:** Rails\
**Created:** [November 18, 2007, 1:46am UTC](https://www.ruby-forum.com/t/variable-inside-of-another-variable/120534 "2007-11-18T01:46:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thiago\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19b73/32.png) [@Thiago\_G](https://www.ruby-forum.com/u/Thiago_G)\
**Post date:** [November 18, 2007, 1:46am UTC](https://www.ruby-forum.com/t/variable-inside-of-another-variable/120534/1 "2007-11-18T01:46:21Z")

</div>

Hi everyone,

I’m building partial and I need to use a variable inside of another  
variable name:

I’ve tried like this:

 \<%= field\_title || field.humanize %\>: \<%= form.text\_field field, :size =\> User::field.upcase\_SIZE, :maxlength =\> User::field.upcase\_MAX\_LENGTH %\> 

like this:

 \<%= field\_title || field.humanize %\>: \<%= form.text\_field field, :size =\> User::#{field.upcase}\_SIZE, :maxlength =\> User::#{field.upcase}\_MAX\_LENGTH %\> 

and like this:

 \<%= field\_title || field.humanize %\>: \<%= form.text\_field field, :size =\> User::{field.upcase}\_SIZE, :maxlength =\> User::{field.upcase}\_MAX\_LENGTH %\> 

To help you understand what I want:

I will call the partial on my view using this code:  
\<% render :partial =\> “text\_field\_row”, :field =\> “email”, :locals =\>  
{ :form =\> form } %\>

and what I want to be displayed in this case is:

 Email: \<%= form.text\_field :email, :size =\> User::EMAIL\_SIZE, :maxlength =\> User::EMAIL\_MAX\_LENGTH %\> 

but I will also use it for another fields rather than just email.

In PHP I know I could simply do it by ${all\_${name}\_text} but how does  
it work with ruby on rails?

Thanks!

---

<div class="post-metadata">

**Author:** ![Thiago\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19b73/32.png) [@Thiago\_G](https://www.ruby-forum.com/u/Thiago_G)\
**Post date:** [November 18, 2007, 1:38pm UTC](https://www.ruby-forum.com/t/variable-inside-of-another-variable/120534/2 "2007-11-18T13:38:19Z")

</div>

On 18 Nov 2007, at 00:45, Thiago G. wrote:

> \<%= form.text\_field :email,  
> :size =\> User::EMAIL\_SIZE,  
> :maxlength =\> User::EMAIL\_MAX\_LENGTH %\>
> 
> but I will also use it for another fields rather than just email.

you probably want to look at const\_get.

Fred

---

<div class="post-metadata">

**Author:** ![Thiago\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19b73/32.png) [@Thiago\_G](https://www.ruby-forum.com/u/Thiago_G)\
**Post date:** [November 19, 2007, 1:09am UTC](https://www.ruby-forum.com/t/variable-inside-of-another-variable/120534/3 "2007-11-19T01:09:13Z")

</div>

Thanks Fred!

I got it working wonderfully with:

\<% field\_title = nil if not defined?(field\_title) -%\>

 \<%= field\_title || field.humanize %\>: \<%= form.text\_field field, :size =\> User::const\_get(field.upcase + "\_SIZE"), :maxlength =\> User::const\_get(field.upcase + "\_MAX\_LENGTH") %\> 

On Nov 18, 10:37 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
