Reputation: 811
I have a Sinatra script, and I have a a common method or set of actions that I use in multiple places and I would like to abstract this out to a method. I am unable to find documentation to help me accomplish this, please does anyone have any ideas?
A typical script looks like this:
require 'sinatra'
get '/' do
'Hello world!'
end
get '/statement' do
'Hello world!'
end
What will be the syntax if I wanted to create a function called greetings()
that displays "Hello world" for both /
and /statement
?
Upvotes: 1
Views: 1404
Reputation: 3741
Sinatra supports a 'helpers' block: http://www.sitepoint.com/using-sinatra-helpers-to-clean-up-your-code/
Upvotes: 1
Reputation: 160551
Have you tried using def
? Sinatra uses a DSL, but that doesn't rule out normal Ruby stuff.
require 'sinatra'
def greetings()
'Hello world!'
end
get '/' do
greetings()
end
get '/statement' do
greetings()
end
Saving that to "test.rb" and running it with ruby test.rb
, then connecting to the running instance at: http://localhost:4567
lets me see either handler respond using greetings()
.
Upvotes: 5