I have lot of helpers in my main Sinatra project_name.rb and I want to remove them to the external file, what is the best practice to do that ?
from ./preject_name.rb
helpers do
...#bunch of helpers
end
to for exapmple ./helpers/something.rb
thank you
Just as you said it yourself:
Move the
helpers
block into another file andrequire
it where you need.Alas, if, like me, you are building a modular Sinatra app, it's a little more complex than simply moving the
helpers
out into another file.The only way I got this to work is as follows.
first up in your app (I'll call this
my_modular_app.rb
)and then create the folder structure
./lib/sinatra/
and createsome_helpers.rb
as follows:doing this you can simply split all your helpers up into multiple files, affording more clarity in larger projects.
The simple and recommended way:
It seems the answer @DaveSag offered miss something. Should add a line at the beginning of
my_modular_app.rb
:In addition, if someone prefers a "classical style" like me, the following is for you :)
In app.rb
In lib/sinatra/some_helpers.rb