From: Brian Candler Date: 2003-07-13T02:08:11+09:00 Subject: Re: do...end vs. begin..end 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. irb(main):002:0> do irb(main):003:1* puts "hello" irb(main):004:1> end SyntaxError: compile error irb(main):005:0> 1.times do irb(main):006:1* puts "hello" irb(main):007:1> end hello 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. Note that 'def' also behaves like 'begin': def mymethod(x) puts "hello #{x}" raise "roof" rescue puts "got exception" end mymethod("world") #>> hello world #>> got exception Regards, Brian.