I have a few local variables in my partials which may or may not be passed by the template that renders them, for instance: on_question_page. If I’m on the page I pass it as true but elsewhere I skip it.
The problem is that I can’t reference that variable directly because in the places it isn’t defined it throws an error.
This means that I end up with a lot of code like this at the top of my partials:
on_question_page = defined?(on_question_page) ? on_question_page : false
Messy. Is there a cleaner way to access these optional variables?
You can use
on_question_page ||= falseto assignfalseifon_question_pageis undefined orfalseornil, that is, something which evaluates tofalsewhen tested with boolean operators.