Hacker News new | ask | show | jobs
by rbanffy 5980 days ago
> Oh, come on. You're just disagreeing for effect here.

It's unclear what "get" is. Is it a function? What does it return? My guess would be that it returns a function bound to the handler of a "/hi" url, but that's a guess.

1 comments

"get" is a function that takes a string and a block.

And the block is bound to the uri given in the string, so your guess is correct.

Ruby always confuses me with this flexible syntax...
It's possible that you just haven't read enough ruby, then. There are only two variations on the syntax of passing a block to a function, "do/end" and "{ }". By convention, multiline blocks use do/end, and single line blocks use {}.

It's also possible that one of the things in the article is the gotcha, flexible parenthesis. By convention, Rubyists tend to use parenthesis when defining functions, but not when calling them:

    def get(path, opts={}, &block)

    get "/hello" do
not

    get("hello") do
Which would still work, if you preferred. The exception to this is chaining method calls:

    Users.find_all_by_age(23).select {|user| user.first_name == "steve" }
... not that that's an awesome use of ActiveRecord, but whatever.