Wednesday 22 May 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

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...