From: daz Date: 2003-07-13T03:23:38+09:00 Subject: Re: do...end vs. begin..end Hi Kurt, "Brian Candler" 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). [#] do...end has lower precedence than {...} when binding to its method. "Programming Ruby - PragProgs" ref: Blocks, Closures, and Proc Objects daz