I’m using Rufus scheduler in a Sinatra app.
Can I access helper methods from within “scheduler do” block? I didn’t manage to do it (I get an “undefined method `check’ for main:Object” error) so now I have to define the method inside helpers block (to use it in “post ‘/’ do” block also) and then copy the method’s body to scheduler block. It does not make sense:( Is there a way to avoid repetition? Can I define a method somewhere else and call it in scheduler?
I’m using Rufus scheduler in a Sinatra app. Can I access helper methods from
Share
It depends where your scheduler is being used. A block will have access to the context local to it, so if you’re using it somewhere you have access to a helper, then it should also have access to the helper.
Taken in part from the docs
Having said that, you wouldn’t have access to the
schedulerlike that, so:and then you can do
or you could put it in a module’s class instance variable:
then you can access the scheduler everywhere via:
but the
my_helperwould still only be accessible in the request scope. If you wanted to access a method as a helper and outside the request scope, then extract it as jmettraux implied to:in Sinatra:
then can do:
All very convoluted! You can mix and match and find what works for your needs.