do-end-blocks

D

Daniel Mendler

Hi!

Why is this code invalid?


respond_to do |format|
format.html
format.xml { render :xml => @books }
format.js { render :update { |p| p.replace_html 'books_table',
:partial => 'books' } } # <---
end

...and this code valid?

respond_to do |format|
format.html # search.html.erb
format.xml { render :xml => @books }
format.js { render :update do |p|; p.replace_html 'books_table',
:partial => 'books'; end }
end

I do not understand when to use do-end blocks and when to use bracket
constructs. According to the documentation they are equivalent...

Daniel
 
C

Christopher Dicely

Hi!

Why is this code invalid?


respond_to do |format|
format.html
format.xml { render :xml => @books }
format.js { render :update { |p| p.replace_html 'books_table',
:partial => 'books' } } # <---
end

...and this code valid?

respond_to do |format|
format.html # search.html.erb
format.xml { render :xml => @books }
format.js { render :update do |p|; p.replace_html 'books_table',
:partial => 'books'; end }
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 Programming Language_ are all good
choices, each with its own particular strengths and weaknesses.
 
D

Daniel Mendler

Hi!
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 Programming 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.

Daniel
 
C

Christopher Dicely

Hi!


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.

The first edition of _Programming Ruby_ is available for free online
at http://www.rubycentral.com/pickaxe/
 
M

Marc Heiler

I only found a lot of examples and tutorials but most of my
questions are quite special and not covered in the tuts.

Go and ask away here, this is a helpful mailing list :)

Or come and stay for a bit on #ruby-lang they are helpful as well.

Currently there is no coherent tutorial. The API docu is ok but
not very pleasant - at least not as pleasant to just write
code in ruby.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,701
Latest member
XavierQ83

Latest Threads

Top