do...end vs. begin..end

K

Kurt M. Dresner

I realize what the difference between these two block delimiters is, but
I can't seem to put it in words. I realize that you only use do..end
when you are passing a block to a method, and that you can use
begin..end to create a block expression with a specific return value
(and rescue/ensure clauses). How would I explain to someone what the
difference is, though? I've been reading through "Programming Ruby" and
they just call both of them "block"s. Anyone with a better way of
explaining it?

-Kurt
 
D

daz

Hi Kurt,

Brian Candler said:
do...end delimits a block, just like { ... }. As far as I know, it can't be
used as a stand-alone construct; the block has to be passed to a method.

That's my (less-experienced) understanding, also.
There is a method in Kernel called 'loop' which provides:

loop do
puts 'something'
break # on some condition
end

but the need rarely arises.
begin...end groups together some expressions/statements. I've never seen
it used except when 'rescue' and/or 'ensure' are also required, because
Ruby's other control structures (e.g. if .. else .. end, while ... end) also
implicitly group statements together.
[...]
Regards,

Brian.

I'll live dangerously and say that begin...end are there for when you
have the requirement, whereas do...end is required almost immediately
and is very common in both its forms (the brace form, as Brian
mentioned above, is [#] identical).

<yawn>
[#] do...end has lower precedence than {...} when binding to its method.
"Programming Ruby - PragProgs" ref: Blocks, Closures, and Proc Objects
</yawn>


daz
 
G

gabriele renzi

il Sat, 12 Jul 2003 19:13:28 +0100, "daz" <[email protected]> ha
scritto::

That's my (less-experienced) understanding, also.
There is a method in Kernel called 'loop' which provides:

loop do
puts 'something'
break # on some condition
end

but the need rarely arises.
well, that is the same thing...try with

loop {
}

this is one of those typical 'other language in ruby' thing :)

I'll live dangerously and say that begin...end are there for when you
have the requirement, whereas do...end is required almost immediately
and is very common in both its forms (the brace form, as Brian
mentioned above, is [#] identical).


just forget 'block':
do..end delimits the code for a Proc object
begin..end enclose a part of your code, mainly to be used with
rescue/ensure statements

my 2c
 
D

daz

gabriele renzi said:
well, that is the same thing...try with

loop {
}

this is one of those typical 'other language in ruby' thing :)

I haven't come from a HLL. I took the choice to use
do...end for all multi-line blocks.
I'd use loop {...} if it was short enough to fit on one line.

IMO, it's more likely an 'other language in ruby' thing
to feel as though 'loop do' is a double operation
-or-
to leave a trailing brace on a line of its own ?
just forget 'block':
do..end delimits the code for a Proc object

my 2c

Then I'd ask you to double your money and use your description
to cover what this does:

proc do
end

I'm comfortable with the description that it converts
a block into a Proc object.


daz
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top