[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

how to do "do" blocks

Daniel Talsky

9/28/2007 8:13:00 AM

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

14 Answers

Jesús Gabriel y Galán

9/28/2007 8:20:00 AM

0

On 9/28/07, 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?

The reason is that "do" starts a block, and then inside the block the
{} are parsed as a hash literal:

irb(main):001:0> collection_of_objects = []
=> []
irb(main):002:0> 10.times do { collection_of_objects << MyObject.create }
irb(main):003:1> end
SyntaxError: compile error
(irb):2: odd number list for Hash
from (irb):3
from :0

You have to use only one form of block, either do...end or {}. The
convention seems to be to use do...end for multiline blocks, while {}
is used for one-liners.

irb(main):005:0> 10.times { collection_of_objects << Array.new }
=> 10
irb(main):006:0> collection_of_objects
=> [[], [], [], [], [], [], [], [], [], []]

Regards,

Jesus.

Daniel Talsky

9/28/2007 8:39:00 AM

0

Well thanks fellas... that makes good sense.

> You have to use only one form of block, either do...end or {}. The
> convention seems to be to use do...end for multiline blocks, while {}
> is used for one-liners.

However, there's some lines of thought on the internet that have a
different idea. What do you think of the idea that it's not "one line
vs. multiline" but "returns a value vs. doesn't return a value".

Here's the two posts I'm talking about:
http://objo.com/2007/6/28/ruby-style-ruby-do-...
http://onestepback.org/index.cgi/Tech/Ruby/BraceVs...

In the first post, one of the commenters asks:
> As for using {} only when returning a value, doesnâ??t every statement return a
> value (in your example, the array with itâ??s elements capitalised and reversed)?

And although he's wrong, the code in question:
i.capitalize!
is in-place changing the variable of course... but his point is still
valid. Anyone want to weigh in on this?
--
Posted via http://www.ruby-....

Jesús Gabriel y Galán

9/28/2007 8:59:00 AM

0

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

> > You have to use only one form of block, either do...end or {}. The
> > convention seems to be to use do...end for multiline blocks, while {}
> > is used for one-liners.
>
> However, there's some lines of thought on the internet that have a
> different idea. What do you think of the idea that it's not "one line
> vs. multiline" but "returns a value vs. doesn't return a value".
>
> Here's the two posts I'm talking about:
> http://objo.com/2007/6/28/ruby-style-ruby-do-...
> http://onestepback.org/index.cgi/Tech/Ruby/BraceVs...
>
> In the first post, one of the commenters asks:
> > As for using {} only when returning a value, doesn't every statement return a
> > value (in your example, the array with it's elements capitalised and reversed)?
>
> And although he's wrong, the code in question:
> i.capitalize!
> is in-place changing the variable of course... but his point is still
> valid. Anyone want to weigh in on this?

I'm fairly new to Ruby, and still trying to accomodate my style
(coming from java it's taking me a while to use do...end, my fingers
type the { automatically :-), but I'm pretty sure I've read many times
here what I said above, and those articles also imply that's the more
widely accepted answer to that question.

I can't comment on other people's uses of this. Maybe more
knowledgeable people here could chime in?

Jesus.

Simon Mullis

9/28/2007 10:54:00 AM

0

I posted this earlier, but it didn't get through... (see my other post
"Ac Cautionary Tale").

hehe

SM

---------- Forwarded message ----------
From: Simon Mullis <simon@mullis.co.uk>
Date: Sep 28, 2007 10:32 AM
Subject: Re: how to do "do" blocks
To: ruby-talk@ruby-lang.org


Hey

The "do..end" and "{..}" are almost equivalent (although "{..}" has
higher precedence).

So,

collection_of_objects = []
10.times do
collection_of_objects << MyObject.create
end

Is the equivalent to:

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


But (from the Ruby Cookbook), you may need to look at precedence.

So,

1.upto 3 do |x|
puts x
end

is ok.

But,

1.upto 3 { |x|
puts x
end

is not, as the code block binds to the 3, not the function call.

So, it would have to be:

1.upto(3) { |x|
puts x
end

Hope this helps!

SM

On 9/28/07, 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?
> --
> Posted via http://www.ruby-....
>
>


--
Simon Mullis
_________________
simon@mullis.co.uk


--
Simon Mullis
_________________
simon@mullis.co.uk

John Joyce

9/28/2007 4:34:00 PM

0

>
> However, there's some lines of thought on the internet that have a
> different idea. What do you think of the idea that it's not "one line
> vs. multiline" but "returns a value vs. doesn't return a value".

All Ruby blocks (and methods) return a value. They return the value
of the last statement in the block.
But only if you explicitly use it.
Assign it to something, or use return if you want it to be obvious.

The convention is simply
{} for one-line
do-end for multi-line

You don't have to follow these rules. It is up to you.

Rick DeNatale

10/2/2007 3:09:00 PM

0

On 9/28/07, Daniel Talsky <danieltalsky@gmail.com> wrote:
> Well thanks fellas... that makes good sense.
>
> > You have to use only one form of block, either do...end or {}. The
> > convention seems to be to use do...end for multiline blocks, while {}
> > is used for one-liners.
>
> However, there's some lines of thought on the internet that have a
> different idea. What do you think of the idea that it's not "one line
> vs. multiline" but "returns a value vs. doesn't return a value".
>
> Here's the two posts I'm talking about:
> http://objo.com/2007/6/28/ruby-style-ruby-do-...
> http://onestepback.org/index.cgi/Tech/Ruby/BraceVs...
>
> In the first post, one of the commenters asks:
> > As for using {} only when returning a value, doesn't every statement return a
> > value (in your example, the array with it's elements capitalised and reversed)?
>
> And although he's wrong, the code in question:
> i.capitalize!
> is in-place changing the variable of course... but his point is still
> valid. Anyone want to weigh in on this?

http://talklikeaduck.denh...articles/2007/10/02/ruby-blocks-d...


--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

Ian J. Ball

5/11/2013 12:08:00 AM

0

On May 10, 4:19 pm, David Johnston <Da...@block.net> wrote:
> On 5/10/2013 4:32 PM, David wrote:
>
> > Development Update: Friday, May 10 - CBS Picks Up Two Dramas, Four
> > Comedies
> > By The Futon Critic Staff (TFC)
>
> > THE CRAZY ONES (CBS/20th; W: David E. Kelley; D: Jason Winer) - The
> > single-camera comedy - about the brilliant, but slightly out of his
> > mind, head of an advertising agency (Robin Williams) and his daughter/
> > creative director (Sarah Michelle Gellar) who's tasked with keeping
> > him in check - has landed a series order at the Eye. Amanda Setton,
> > Hamish Linklater and James Wolk also star. (Deadline.com)
>
> Oh, it's a DEK series.  Too bad.

Yeah, I didn't catch that - that makes me a *lot less* likely to
sample, even with SMG and Amanda Setton and James Wolk...

David Johnston

5/11/2013 12:18:00 AM

0

On 5/10/2013 5:33 PM, Obveeus wrote:
> "David Johnston" <David@block.net> wrote:
>
>> On 5/10/2013 4:32 PM, David wrote:
>>> WE ARE MEN (A.K.A. EX MEN) (CBS/CTS; W/D: Rob Greenberg) - Rob
>>> Greenberg's long-in-the-works comedy has landed a series order from
>>> the Eye. The single-camera project revolves around a twentysomething
>>> guy (Chris Smith), who finds camaraderie living among three more
>>> experienced men (Jerry O'Connell, Kal Penn and Tony Shalhoub) he meets
>>> in a short-term rental complex. Rebecca Breeds also stars.
>>> (Deadline.com)
>>
>> It's a 4 guys show. Why do they keep pitching 4 guys shows? Has there
>> ever been a successful 4 guys show?
>
> BARNEY MILLER, M*A*S*H...or are you not going to include workplace comedies?

I'm not. Beside those shows didn't have 4 guys. Barney Miller had 6
guys. Mash really only had one guy, 7 if you counted supporting cast.
But the 4 guys shows don't have common workplace issues to deal with.
It's about their male bonding in the face of bizarre alien creatures.
And by that I mean "women". Or in one case "babies".

> SEINFELD started as a 3 guy show with the gal added as an afterthought.
>
>

I can think of a few 3 guys and one girl shows that were pretty
successful. Just no 4 guys shows.

Anim8rFSK

5/11/2013 1:29:00 AM

0

In article
<528e2148-04fe-4ac0-a2d7-3867814f6385@ul7g2000pbc.googlegroups.com>,
"Ian J. Ball" <ijball@mac.com> wrote:

> On May 10, 4:19?pm, David Johnston <Da...@block.net> wrote:
> > On 5/10/2013 4:32 PM, David wrote:
> >
> > > Development Update: Friday, May 10 - CBS Picks Up Two Dramas, Four
> > > Comedies
> > > By The Futon Critic Staff (TFC)
> >
> > > THE CRAZY ONES (CBS/20th; W: David E. Kelley; D: Jason Winer) - The
> > > single-camera comedy - about the brilliant, but slightly out of his
> > > mind, head of an advertising agency (Robin Williams) and his daughter/
> > > creative director (Sarah Michelle Gellar) who's tasked with keeping
> > > him in check - has landed a series order at the Eye. Amanda Setton,
> > > Hamish Linklater and James Wolk also star. (Deadline.com)
> >
> > Oh, it's a DEK series. ?Too bad.
>
> Yeah, I didn't catch that - that makes me a *lot less* likely to
> sample, even with SMG and Amanda Setton and James Wolk...

And are they hiring Robin Williams from 1980?

--
"Every time a Kardashian gets a TV show, an angel dies."

Obveeus

5/11/2013 2:34:00 AM

0


"anim8rFSK" <anim8rfsk@cox.net> wrote:
> "Ian J. Ball" <ijball@mac.com> wrote:
>> On May 10, 4:19 pm, David Johnston <Da...@block.net> wrote:
>> > > THE CRAZY ONES (CBS/20th; W: David E. Kelley; D: Jason Winer) - The
>> > > single-camera comedy - about the brilliant, but slightly out of his
>> > > mind, head of an advertising agency (Robin Williams) and his
>> > > daughter/
>> > > creative director (Sarah Michelle Gellar) who's tasked with keeping
>> > > him in check - has landed a series order at the Eye. Amanda Setton,
>> > > Hamish Linklater and James Wolk also star. (Deadline.com)
>> >
>> > Oh, it's a DEK series. Too bad.
>>
>> Yeah, I didn't catch that - that makes me a *lot less* likely to
>> sample, even with SMG and Amanda Setton and James Wolk...
>
> And are they hiring Robin Williams from 1980?

This month's Playboy Magazine has an interview with J.J.Abrams in which he
mentions what it was like as a kid to go on set and see Robin Williams out
of character. He had even less nice things to say about his visit to the
EIGHT IS ENOUGH set. I know you aren't a J.J. Abrams fan, but it is a good
read:
http://www.playboy.com/playground/view/playboy-interview-j-j-abrams-on-star-trek...