f

#### do...end vs. begin..end

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


 0
kdresner (65)
7/11/2003 7:46:21 PM
comp.lang.ruby 48886 articles. 0 followers.

3 Replies
450 Views

Similar Articles

[PageSpeed] 31

Hi Kurt,

"Brian Candler" <B.Candler@pobox.com> wrote:
> On Sat, Jul 12, 2003 at 04:46:21AM +0900, Kurt M. Dresner wrote:

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


 0
dooby1 (502)
7/12/2003 6:13:28 PM
il Sat, 12 Jul 2003 19:13:28 +0100, "daz" <dooby@d10.karoo.co.uk> 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

 0
surrender_it (690)
7/12/2003 7:00:52 PM
"gabriele renzi" <surrender_it@remove.yahoo.it> wrote:

> il Sat, 12 Jul 2003 19:13:28 +0100, "daz" <dooby@d10.karoo.co.uk>
> ha scritto::
>
> >
> >  loop do
> >  end
> >
>
> 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

to cover what this does:

proc do
end

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

daz
--
(not forgetting 'block' in too much of a hurry :)


 0
dooby1 (502)
7/13/2003 4:17:36 AM

Similar Artilces:

The beginning of the end, or the end of the beginning?
http://www.sltrib.com/ci_4747497 The beginning of the end, almost certainly. The cases will drag on into 2008 irrespective, but I'd be very surprised if SCO itself doesn't gone into administration during 2007. They must be almost out of cash by now, and all their assets are probably owed to Novell anyway. We should know more about the cash situation in a couple of weeks. On Sun, 3 Dec 2006, jezlinux wrote: > The beginning of the end, almost certainly. The cases will drag on > into 2008 irrespective, but I'd be very surprised if SCO itself doesn't > gone int...

how to redefine \begin{align*} ... \end{align*} to \begin{eqnarray*} .... \end{eqnarray*}
Hello; I am not good with Latex syntax. I think I need to use \renewenvironment, but I am not getting the syntax right. Scientific word insists in generating displayed math using \begin{align*} ... \end{align*} and htlatex has a bug in that it does not generate bit mapped images in this case, so that math looks ugly. The fix that I found after trial and error is to replace the above with \begin{eqnarray*} .... \end{eqnarray*} So I assume I need to put something in the preamble to tell it that. (I know how to make sure this is effective only only htlatex is running ...

Begin/rescue/end versus do/end
These don't seem orthogonal. It feels like I should be able to do obj.meth do |x| #... rescue SomeException => ex #... end and that either begin should be something like a Kernel method: def begin yield end (although maybe with optional "do"), or blocks could be standalone expressions if not part of a method call (this doesn't _seem_ to cause ambiguity, but someone cleverer could probably find some). So I could do do #... rescue SomeException => ex #... end which behaves just like "begin", and I wouldn't have to use blocks in some places (ite...

Hop-by-Hop vs End-to-End
Hi, When we try to communicate with another PC the source computer sends an arp to the destination and in turn the destination sends its MAC, thus establishing a direct path for communication. From this it seems that MAC is hop-by-hop and IP is end-to-end, but from different texts it seems IP is hop-by-hop and MAC is end-to-end. Please help In article <1107754186.525418.259250@c13g2000cwb.googlegroups.com>, <rohri@hotmail.com> wrote: :When we try to communicate with another PC the source computer sends an :arp to the destination and in turn the destination sends its ...

Back-End Vs Front-End
Hi, What is the norm when a front-end programmer (e.g VB) designs an interface for a database (e.g Oracle)? Specifically, I mean should the front-end programmer hard-code constraints like null/nullable fields into his/her interface, or should he/she just output the meaningless Oracle errors to the user? (e.g via the VB interface). I was thinking there are good and bad things about the front-end programmer hard-coding/duplicating the "controls" into their program. The bad: if the DBA un-nulls a field on the Oracle DB, the front-end will still insist on a input of dat...

High end vs low end platforms
This is a serious enquiry, sorry if it comes across as a bit of a rant. I�ve done two projects over the last 6 months, both with a similar target end result, a real time device for processing MIDI messages from musical equipment. The first implementation used an HCS12, it took just over a month to stage the hardware, and to prototype and complete the software from the ground up. That included a basic RTOS with multilevel IRQ processing, a rudimentary file system and transfer through a serial connection, and interfacing with a couple of on-bus and SPI devices. The final system came tog...

back-end vs. front-end calculation
Hi all, Let's say I have a simple math formula: sum (x * y / 1000) / (sum z / 1000) I have to do this across 50 items, each with an x, y and z value, when the page first loads AND when a user modifies any of the x, y and z values. Needless to say, I need to have a Javascript function that handles this formula to deal with the modifications. However, I have a choice on page load to either have the database in which this data is initially stored perform this calculation or to have the same Javascript function that will handle the onchange event handle the initial calculation as well. a...

SIP and end-to-end
Common wisdom says that for a SIP-based telephony session the media stream is directly end to end. However, for both FWD and sipgate.de I figured that the media stream goes via the respective provider. I would assume the sip proxy to manipulate the Invite messages accordingly. Is this being done to better cope with NAT devices and firewalls? -- Georg Schwarz http://home.pages.de/~schwarz/ georg.schwarz@freenet.de +49 178 8545053 Hello, > Common wisdom says that for a SIP-based telephony session the media > stream is directly end to end. > However, for both FWD a...

End-to-End VOIP
I just bought a pair of Grandstreams Budgettone 100's for \$65 from Pulver for a cross-country test. Boy am I impressed. The test was between the SF, CA area to Northampton, MA. The most striking thing was how quiet the connection was. (Sort of like going from phonographs to well-recorded CD's.) The fact that the phone doesn't need any 2-wire to 4-wire hybrid can't hurt either. Lets hear it for true uni-directional talk paths without any transformers in the way to saturate and add other distortions and coloration. The raw ping times between the endpoints was noisy wi...

end to end security
hi what exactly does end to end security mean?? is SSL considered as an end to end security product? Besides using SSL in web servers/browsers, in what other ways can we make use of SSL? can it be used in file transfers and how to do it?? thanks mike wrote: > > hi > what exactly does end to end security mean?? is SSL considered as an > end to end security product? End-to-end means that the communicating parties en/decrypt themselves. No party along the path is able to decrypt the messages. In this sense SSL is end-to-end. > Besides using SSL in web servers/browsers, in what...

This is the End... My Only Friend, The End...
Hi Pals, I got two days ago a phone call from a friend from SW Engineering in the U.S. He spent 29 years and six months at DEC/CPQ/HP (PDP, VAX, Alpha, Itanium). He terminated his current project last week, then was called by his manager. The manager said (more or less) "good job, now, I'm sorry but "Business has changed" (sic). You may leave the Company. Good bye, good luck and thank you for all the coding". I do not think that a Company can fire people from Engineering with such skills if the Product is not going to be phased out soon. D. -- Didier MORANDI...

High-end AVR vs. low-end ARM?
In terms of pricing, how do high-end AVR's (Mega-128) compare to low-end ARM processors? The ARM's are much more powerfull and have large RAM memories on them. Anyone ever compare them? I heard that ARM's are cheaper than AVR's these days. Is this true? On 6 Nov, 21:42, "Bresco" <bre...@mixmaster.org> wrote: > In terms of pricing, how do high-end AVR's (Mega-128) compare to low-end ARM > processors? The ARM's are much more powerfull and have large RAM memories on > them. > > Anyone ever compare them? I heard that ARM's are chea...

LaTeX Error: \begin{document} ended by \end{indented}.
Dear all, I got this error message, when I was compiling my .tex: LaTeX Error: \begin{document} ended by \end{indented}. The output seems to be okay, though. Any hint? Best Regards, Zulfikar Dharmawan "Zulfikar Dharmawan" <zulfikar.dharmawan@gmail.com> writes: >I got this error message, when I was compiling my .tex: > LaTeX Error: \begin{document} ended by \end{indented}. > >The output seems to be okay, though. Any hint? there's an isolated \end{indented} in your document (one not matched by \begin{indented}, that is). text after the \end{indented} wil...

end to end testing
when doing end to end testing wheather we stop the testing and contact some other testing? anand4180 wrote: > when doing end to end testing wheather we stop the testing and > contact some other testing? You never stop testing. You "analyze" (meaning "guess") how much of each kind to do, and then you release whenever the program passes all tests and has new features. And most of your tests should be automated. Again, you "analyze" to discover how many. -- Phlip http://www.greencheese.us/ZeekLand <-- NOT a blog!!! On Mar 17, 6:13 am, &qu...

Web resources about - do...end vs. begin..end - comp.lang.ruby

Nolan Nawrocki Went All-In On Cam Newton and Lost
Nolan Nawrocki isn’t shy when expressing his opinion and as Super Bowl 50 approaches that may have come back to bite him. The former Pro Football ...

Hot iPad Air 3 rumor might make you care about tablets again
When was the last time you really cared about tablets? If you're like me, it was probably around the time that Apple unveiled its third-generation ...

High-speed Internet without the cables
Starry is a new company that is promising to bring high-speed Internet over the air. The company says it can deliver gigabit speed Internet that ...

Oscars‪‪/Academy Awards 2016 Controversy: President Barack Obama And Hillary Clinton Weigh In
President Barack Obama speaking on the recent 2016 Oscar/Academy Award nominations and backlash is evidence that the Oscars have has created ...

Apple plug recall over safety concerns
Apple issues a voluntary recall of millions of its two-prong AC wall plugs after incidents of them breaking and causing electric shocks.

Arrested Oregon occupation leader tells everyone to go home
Ammon Bundy, the leader of the armed occupation of a Federal wildlife reserve in Oregon, has told the rest of them to leave it following his ...

Kanye West's Insane Rants Directly Linked to LeBron James Playing Good Basketball
Kanye inspired me earlier. I decided to research Lebron's averages following a Yeezy rant. Here are the numbers! pic.twitter.com/j04wQbidSg ...

Zika virus is spreading 'explosively' and may be international health emergency, WHO warns
The World Health Organization (WHO) warned Thursday that the mosquito-borne Zika virus is spreading “explosively” throughout the Americas, and ...

The Challenger Explosion On January 28, 1986 Was A Day History Unfolded On Live TV
The explosion on January 28, 1986, of the space shuttle Challenger was a “where were you when?” moment. That day, when millions of people watched ...

Flint Has No Plans to Replace Its Corroded, Lead-Filled Water Pipes
The corroded lead water pipes ferrying tainted tap water to the city of Flint, MI will stay as is for now, officials said this week. Read more... ...

Resources last updated: 1/28/2016 5:32:42 PM