How do I check if a variable is defined in rails?

cjm2671 picture cjm2671 · Oct 18, 2011 · Viewed 32.8k times · Source
<% if dashboard_pane_counter.remainder(3) == 0 %>
  do something
<% end>

If dasboard_pane_counter wasn't defined, how can I get this to evaluate to false rather than throw an exception?

Answer

Matt picture Matt · Oct 18, 2011
<% if defined?(:dashboard_pane_counter) && dashboard_pane_counter.remainder(3) == 0  %>
  # do_something here, this assumes that dashboard_pane_counter is defined, but not nil
<% end %>