# Variable variable names... are you confused?

**URL:** <https://www.ruby-forum.com/t/variable-variable-names-are-you-confused/162186>\
**Category:** Rails\
**Created:** [March 6, 2009, 3:14pm UTC](https://www.ruby-forum.com/t/variable-variable-names-are-you-confused/162186 "2009-03-06T15:14:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rsm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4af34b/32.png) [@rsm](https://www.ruby-forum.com/u/rsm)\
**Post date:** [March 6, 2009, 3:14pm UTC](https://www.ruby-forum.com/t/variable-variable-names-are-you-confused/162186/1 "2009-03-06T15:14:00Z")

</div>

Hi Guys,

I got a helper method that I want to use more than once. The only thing  
that will change will be the variable that is used to collect the data.  
For example;

def render\_blocks  
position = 0  
@variable.collect { |question, answer|  
position += 1  
“

### #{position}. \<a name=”#{block\_to\_anchor(question)}"\>#{question}
" +  
content\_tag(‘div’, simple\_format(answer))  
}  
end

I need to call this helper method from different pages. Can I pass  
something through to the helper method that changes the variable name to  
the one I want…

For example

\<%= render\_blocks :page =\> ‘free’ %\> on one page that changed @variable  
to @free and  
\<%= render\_blocks :page =\> ‘faq’ %\> that changes @variable to @faq.

Any help would be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![rsm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4af34b/32.png) [@rsm](https://www.ruby-forum.com/u/rsm)\
**Post date:** [March 6, 2009, 3:23pm UTC](https://www.ruby-forum.com/t/variable-variable-names-are-you-confused/162186/2 "2009-03-06T15:23:44Z")

</div>

On Fri, Mar 6, 2009 at 3:14 PM, Roger M.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Â Â Â position += 1  
> Â Â Â “
> 
> ### #{position}. \<a name="#{block\_to\_anchor(question)}"\>#{question}
> ” +  
> Â Â Â Â content\_tag(‘div’, simple\_format(answer))  
> Â Â }  
> end
> 
> I need to call this helper method from different pages. Can I pass  
> something through to the helper method that changes the variable name to  
> the one I want…

Just declare a parameter in your helper method Ã la:

def render\_blocks(my\_variable)  
my\_variable.collect  
end

You then call it in your view and pass the variable that is valid for  
that view.

\<%= render\_blocks(@free) %\>

---

<div class="post-metadata">

**Author:** ![rsm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4af34b/32.png) [@rsm](https://www.ruby-forum.com/u/rsm)\
**Post date:** [March 6, 2009, 3:56pm UTC](https://www.ruby-forum.com/t/variable-variable-names-are-you-confused/162186/3 "2009-03-06T15:56:04Z")

</div>

> Just declare a parameter in your helper method Ã la:
> 
> def render\_blocks(my\_variable)  
> my\_variable.collect  
> end
> 
> You then call it in your view and pass the variable that is valid for  
> that view.
> 
> \<%= render\_blocks(@free) %\>

grand. cheers
