[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: how to do "do" blocks

Tom

9/28/2007 8:21:00 AM

Take the "do" out of the statement when passing a block in {}:

collection_of_objects = []
10.times {collection_of_objects << MyObject.create}

:)


On 9/28/07 2:13 AM, "Daniel Talsky" <danieltalsky@gmail.com> wrote:

> How come this works:
>
> collection_of_objects = []
> 10.times do
> collection_of_objects << MyObject.create
> end
>
> But this doesn't:
>
> collection_of_objects = []
> 10.times do { collection_of_objects << MyObject.create }
>
> I didn't understand there was ANY difference between the two syntaxes
> really. Can someone explain to me the finer points?



2 Answers

Bertram Scharpf

9/28/2007 10:03:00 AM

0

Hi,

Am Freitag, 28. Sep 2007, 17:20:40 +0900 schrieb Tom Metge:
> On 9/28/07 2:13 AM, "Daniel Talsky" <danieltalsky@gmail.com> wrote:
>
> > 10.times do
> > collection_of_objects << MyObject.create
> > end
> >
> > But this doesn't:
> >
> > 10.times do { collection_of_objects << MyObject.create }
>
> Take the "do" out of the statement when passing a block in {}:
>
> 10.times {collection_of_objects << MyObject.create}

Yet another opportunity to mention that {} has higer
precedence than do..end.

def f
puts "((("
yield if block_given?
puts ")))"
end
def g x
puts "[[[#{x}|"
yield if block_given?
puts "]]]"
end

> g f { puts "x" }
(((
x
)))
[[[|
]]]
> g f do puts "x" end
(((
)))
[[[|
x
]]]


Not a very intuitive example I admit.

Bertram


--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-...

David A. Black

10/3/2007 11:55:00 AM

0