Newbie wrote:

> I'm a new to Ruby, and struggling to understand the different kinds of
> blocks/procs/methods/lambadas etc.
> 
> I found a good intro at
>
http://eli.thegreenplace.net/2006/04/18/understanding-ruby-blocks-procs-and-methods/,
> but one thing which it doesn't cover is begin/end. Why is a new keyword
> used, instead of adding rescue/else/ensure to do/end blocks?

To put it in simplest terms, the keyword "do" must be preceded by a data
source, while the keyword "begin" doesn't. So "begin" meets a syntactical
requirement that "do" doesn't.

Maybe this will help:

# valid:

1.upto(10) do |x|
   puts x
end

# not valid:

do |x|
   puts x
end

# valid:

begin
   # normal process
rescue  
   # deal with errors
else
   # no-errors section
ensure 
   # always-run section
end

# also valid:

x = 0

begin
   puts x
   x += 1
end while x <= 10

# also valid:

x = 0

begin
   puts x
   x += 1
end until x > 10

# even this works:

x = 0

begin
   puts x
   x += 1
   x /= 0 if x == 8 # generate an error
rescue
   puts "Error!"
end until x > 10

The last form emits this:

0
1
2
3
4
5
6
7
Error!
8
9
10


HTH

-- 
Paul Lutus
http://www.arachnoid.com