Question

I really have no idea what to search for to get the answer to this:

Is there a short hand for this block:

get '/' do
  501
end

I got started by following the rubymonk.com tutorial and it implied there was generally common-sense shorthand to most things in ruby but I tried the following with no luck:

get '/' 501
get '/' (501)
get '/' {501}
get '/' do 501
Was it helpful?

Solution 2

Try this:

get('/') { 501 }

Seems to be an issue with Ruby syntax.

get '/' { 501 } (f g x) seems to be interpreted as get('/'({ 501 })) (f(g(x))) which is not correct.

OTHER TIPS

Don't know what version of Ruby you are using, maybe 1.8.7, on Ruby 1.9 and 2.1.0 the error reported for the code get '/' { 501 } is:

syntax error, unexpected '{', expecting $end

The cause is that the parser tries to tie the block { 501 } to the literal '/', i.e. the code is parsed like get('\' { 501 }), that is obviously an error. You just have to disambiguate the syntax by putting parentheses around the '\' argument:

get('/') { 501 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top