# Listing out available variables in an ERB template

**URL:** <https://www.ruby-forum.com/t/listing-out-available-variables-in-an-erb-template/195619>\
**Category:** Ruby\
**Created:** [September 27, 2010, 3:16am UTC](https://www.ruby-forum.com/t/listing-out-available-variables-in-an-erb-template/195619 "2010-09-27T03:16:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 27, 2010, 3:16am UTC](https://www.ruby-forum.com/t/listing-out-available-variables-in-an-erb-template/195619/1 "2010-09-27T03:16:08Z")

</div>

Hi All,

Suppose I have a Ruby ERB template named **my\_template.html.erb** , and  
it  
contains the following:

```
<div><%= @div_1 %></div>
<div><%= @div_2 %></div>
<div><%= @div_3 %></div>

```

Is there a way I can programatically list out all the available  
variables in  
the template?

For example, the following method:

```
def list_out_variables
  template = File.open("path_to/my_template.html.erb", "rb").read
  erb = ERB.new( template )
  erb.this_method_would_list_out_variables
end

```

would return something like:

```
['div1','div2','div3']

```

Any help would be greatly appreciated.

Thanks,  
Mike

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 27, 2010, 5:55am UTC](https://www.ruby-forum.com/t/listing-out-available-variables-in-an-erb-template/195619/2 "2010-09-27T05:55:21Z")

</div>

On Sun, Sep 26, 2010 at 8:12 PM, removed\_email\_address@domain.invalid \<  
removed\_email\_address@domain.invalid\> wrote:

> in  
> would return something like:
> 
> [‘div1’,‘div2’,‘div3’]
> 
> Any help would be greatly appreciated.
> 
> Thanks,  
> Mike

Ruby has some reflexive methods you can use.

$gvar = 1  
lvar = 2  
@ivar = 3  
class Foo  
@@cvar = 4  
end

p instance\_variables  
p global\_variables  
p local\_variables  
class Foo  
p class\_variables  
end
