From: Josh Cheek Date: 2010-09-27T12:51:04+09:00 Subject: Re: listing out available variables in an ERB template --001485f4c08aff6346049135a215 Content-Type: text/plain; charset=ISO-8859-1 On Sun, Sep 26, 2010 at 8:12 PM, mgerstenblatt@gmail.com < mgerstenblatt@gmail.com> wrote: > Hi All, > > Suppose I have a Ruby ERB template named **my_template.html.erb**, and it > contains the following: > >
<%= @div_1 %>
>
<%= @div_2 %>
>
<%= @div_3 %>
> > 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 > 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 --001485f4c08aff6346049135a215--