From: Clemens Hintze Date: 2002-03-26T17:46:27+09:00 Subject: Re: Difference between 'do' and 'begin' In Wladimir Mutel writes: > Hi, Hi too, > I started to learn Ruby (having knowledge of some other languages > before), and noted two similarly looking keywords in it. Why there is > a need to make two of them, not one, to start a block ? The joke is, that you are right and wrong at the same place ;-) Although there *are* two 'keywords' to open a block, but those are 'do' and '{'. Both have different precedence, so that both following lines have different meaning: - anyMethod1 anyMethod2 do ... end - anyMethod1 anyMethod2 { ... } The first line associate the block to anyMethod1, whereas the second invocation associate the block to the call of anyMethod2 ... 'begin' is a totally different construct, that is *not* building a block. It is more a statement like 'if' or 'while' ... - A block, e.g., can be converted into a closure. You cannot do this with 'begin' started code. - A block opens an own scope for variables that were created within the block. And it reuse variables that were created outside. 'begin' do *not* open a new scope. All variables crated within are accessible outside. 'begin' is most often used together with its 'rescue' and/or 'ensure' part to catch and handle exceptions that were raised by code within. Regards, \cle -- Clemens Hintze mailto: c.hintze@gmx.net