I was looking at Sinatra and trying to understand the syntax:
require 'sinatra'
get '/' do
"Hello, World!"
end
I understand it does this:
This is a ‘Route’. Here, we’re telling Sinatra that if the home, or root, URL ‘/’ is requested, using the normal GET HTTP method, to display “Hello, World!”
But what is happening the Ruby language?
- What does this syntax mean:
get '/'? Isgeta method and ‘/‘ a parameter to it? If it is method, then in Ruby, I can call a method asmethodname (parameter) {}. What is{ }there for? - I usually understand
doandendas{ }, which are kinds of enclosures to function bodies. - Between
doandendwe have “Hello, World!” so is it a statement? What I mean is, it is getting printed, but we did not call it asprint "Hello, World!", so what is going on? - It seems
getis a method defined in Sinatra, but if I add a gem, where there is agetmethod already defined, then how do I know which ‘get’ method it would call? Or, does it refer to the HTTPgetmethod?
I am sorry if this question sounds very basic, but I want to get through it before I move forward.
May I suggest going through a tutorial on ruby before tackling a larger problem like
sinatrawhich is a fairly specialized library.A good place to start is with the Ruby Koans
As for your questions.
getis a method.'/'is its argument. anddo ... enddenotes a block in ruby just like{}would.do ... endarereturn "String".getis thesinatradefined methodget. Abstractly it stands for anHTTP GETrequest against the server.