<% 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?
<% 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?
 
    
    <% 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 %>
 
    
    local_assigns can be used for that, since this question is from a few years ago, I verified that it exists in previous versions of rails
<% if local_assigns[:dashboard_pane_counter] 
                 && dashboard_pane_counter.remainder(3) == 0%>
<% end %>
It's in the notes here
 
    
    When using rails and instance variables, nil has a try method defined, so you can do:
<% if @dashboard_pane_counter.try(:remainder(3)) == 0  %>
   #do something
<% end %>
so if the instance variable is not defined, try(:anything) will return nil and therefore evaluate to false. And nil == 0 is false
 
    
    Posting this answer for beginner coders like myself. This question can be answered simply using two steps (or one if using &&). It is a longer and less pretty answer but helps new coders to understand what they are doing and uses a very simple technique that is not present in any of the other answers yet. The trick is to use an instance (@) variable, it will not work with a local variable:
if @foo
  "bar"
end
If @foo is defined it will be return "bar", otherwise not (with no error). Therefore in two steps:
if @dashboard_pane_counter
  if @dashboard_plane_counter.remainder(3) == 0
    do something
  end
end
 
    
    Insted of
if !var.nil?
I would use
unless var.nil?
Thats much better ruby code!
