Block syntax inconsistency or my confusion

Hi,

I am sorry if this is too simple or has been answered but I haven’t
found a good enough explanation yet, so here it goes:

It seems to me that the use of do-end syntax in expressions, is
inconsistent as it means that it does not always signify a code block.
Furthermore, I am not sure whether it is only expressions that have an
exceptional meaning for do-end blocks. This is significant, because of
local variable scoping.

Specifically, a [while|for|unitil|other?] do-end does not
define a local scope although it seems like it could be as every other
use of do-end does.

Is there some list of exceptional uses of do-end?

TIA,
Kosta

do is optional, and there’s no need for it if you’re starting a new
line anyways.

while true
puts “hello”
end

if true
puts “hello”
end

But do is not optional for blocks:

5.times do
puts “hello”
end

This is equivalent to

5.times {
puts “hello”
end

But you get a syntax error when you write

while {
puts “hello”
}

[while|if|…] are keywords, while Integer#times are methods that take a
block as an argument.

http://ruby-doc.org/docs/keywords/1.9/