Wednesday, May 22, 2019

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



<% 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



<% 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 %>

No comments:

Post a Comment

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...