[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

do-end-blocks

Daniel Mendler

3/8/2008 2:33:00 AM

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


10 Answers

Christopher Dicely

3/8/2008 2:54:00 AM

0

On Fri, Mar 7, 2008 at 6:32 PM, Daniel Mendler <dmendler@wurzelteiler.de> wrote:
> 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.

Daniel Mendler

3/8/2008 3:06:00 AM

0

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


Christopher Dicely

3/8/2008 3:43:00 AM

0

On Fri, Mar 7, 2008 at 7:05 PM, Daniel Mendler <dmendler@wurzelteiler.de> wrote:
> 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.
>

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

Marc Heiler

3/8/2008 5:19:00 AM

0

> 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.
--
Posted via http://www.ruby-....

Nigel Bruin

3/8/2008 7:34:00 AM

0

On 8 Mar 2008, at 11:42, Christopher Dicely wrote:
> The first edition of _Programming Ruby_ is available for free online
> at http://www.rubycentral.co...

Now dead links?, but seems alive and well at:
http://www.ruby-doc.org/docs/Programming...

Nigel Bruin.

Butterflygirl24

11/19/2010 9:41:00 PM

0

On Nov 19, 2:27 pm, ovfdfireman <ovfdfire...@hotmail.com> wrote:
> On Nov 17, 9:18 pm, Tom G <tgr...@gmail.com> wrote:
>
> > Hey Since were're talking Pingame journal.  Has everyone recieved the
> > new "sept" issue with Avatar on the cover.  I have not recieved it
> > yet.
>
> > Tom.
>
> I got mine

I haven't gotten mine yet either! ;-)

Phoebe
CARGPB#38

Ned Ledod

11/19/2010 11:38:00 PM

0

On Nov 17, 9:18 pm, Tom G <tgr...@gmail.com> wrote:
> Hey Since were're talking Pingame journal.  Has everyone recieved the
> new "sept" issue with Avatar on the cover.  I have not recieved it
> yet.
>
> Tom.

Mine came Wednesday.

Basil

11/19/2010 11:47:00 PM

0

I just got mine in todays mail.

Max Badazz

11/20/2010 4:06:00 AM

0

> Hey Since were're talking Pingame journal.  Has everyone recieved the
> new "sept" issue with Avatar on the cover.

I got mine on Tuesday.

Hopefully my Avatar article will be in the next issue :-)

Chris (in NH)
http://usergallery.myhomegameroom.com/gallery...

Tom G

11/20/2010 4:19:00 AM

0

On Nov 17, 9:18 pm, Tom G <tgr...@gmail.com> wrote:
> Hey Since were're talking Pingame journal.  Has everyone recieved the
> new "sept" issue with Avatar on the cover.  I have not recieved it
> yet.
>
> Tom.

Still haven't recieved mine but there is always tomorrow.