end
I do not understand when to use do-end blocks and when to use bracket
constructs.
They are not equivalent: they mean the same thing, but blocks
delineated by braces bind more tightly than do … end blocks. This is
an issue when you have parameters to a method call that are not
enclosed with parentheses. In your snippet this:
render :update { |p| … }
Is not equivalent to:
render :update do |p| … end
But instead to:
render (:update do |p| … end)
Since :update is a Symbol and can’t take a block, this is invalid.
You could still use braces, but you would have to parenthesize the
argument to render, as so:
render(:update) {|p| … }
According to the documentation they are equivalent…
Which documentation? Your code seems to be Rails code, and its true
that the very minimal introduction to Ruby in Appendix A of Agile Web
Development with Rails doesn’t seem to cover this difference in the
section on blocks. If you are trying to learn Ruby and Rails
simultaneously, I would suggest you to refer to a good general Ruby
book along with AWDR; AWDR introduces Rails quite well, but its
coverage of Ruby is vestigial. Programming Ruby, The Ruby Way, Ruby for Rails, and The Ruby P.ming Language are all good
choices, each with its own particular strengths and weaknesses.
Thank you for the clarification concering the block! But my problem with
ruby is that there seems to exist no free specification. I really don’t
want to buy such a book (I don’t use awdr either). Please correct me if
there exist free specs except the api docs. I only found a lot of
examples and tutorials but most of my questions are quite special and
not covered in the tuts.