# Do-end-blocks

**URL:** <https://www.ruby-forum.com/t/do-end-blocks/131322>\
**Category:** Ruby\
**Created:** [March 8, 2008, 3:33am UTC](https://www.ruby-forum.com/t/do-end-blocks/131322 "2008-03-08T03:33:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![danmcb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@danmcb](https://www.ruby-forum.com/u/danmcb)\
**Post date:** [March 8, 2008, 3:33am UTC](https://www.ruby-forum.com/t/do-end-blocks/131322/1 "2008-03-08T03:33:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![danmcb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@danmcb](https://www.ruby-forum.com/u/danmcb)\
**Post date:** [March 8, 2008, 3:54am UTC](https://www.ruby-forum.com/t/do-end-blocks/131322/2 "2008-03-08T03:54:35Z")

</div>

On Fri, Mar 7, 2008 at 6:32 PM, Daniel M.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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.

---

<div class="post-metadata">

**Author:** ![danmcb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@danmcb](https://www.ruby-forum.com/u/danmcb)\
**Post date:** [March 8, 2008, 4:06am UTC](https://www.ruby-forum.com/t/do-end-blocks/131322/3 "2008-03-08T04:06:18Z")

</div>

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.

Daniel

---

<div class="post-metadata">

**Author:** ![danmcb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@danmcb](https://www.ruby-forum.com/u/danmcb)\
**Post date:** [March 8, 2008, 4:43am UTC](https://www.ruby-forum.com/t/do-end-blocks/131322/4 "2008-03-08T04:43:47Z")

</div>

On Fri, Mar 7, 2008 at 7:05 PM, Daniel M.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > coverage of Ruby is vestigial. _Programming Ruby_, _The Ruby Way_,

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

---

<div class="post-metadata">

**Author:** ![danmcb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@danmcb](https://www.ruby-forum.com/u/danmcb)\
**Post date:** [March 8, 2008, 6:19am UTC](https://www.ruby-forum.com/t/do-end-blocks/131322/5 "2008-03-08T06:19:20Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![danmcb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@danmcb](https://www.ruby-forum.com/u/danmcb)\
**Post date:** [March 8, 2008, 8:34am UTC](https://www.ruby-forum.com/t/do-end-blocks/131322/6 "2008-03-08T08:34:33Z")

</div>

On 8 Mar 2008, at 11:42, Christopher D. wrote:

> The first edition of _Programming Ruby_ is available for free online  
> at [http://www.rubycentral.com/pickaxe/](http://www.rubycentral.com/pickaxe/)

Now dead links?, but seems alive and well at:  
[http://www.ruby-doc.org/docs/ProgrammingRuby/html/](http://www.ruby-doc.org/docs/ProgrammingRuby/html/)

Nigel Bruin.
