ruby picture

RCR 299: Indented code blocks

Submitted by marian (Sun Apr 03 21:03:18 UTC 2005)

Abstract

Ruby syntax is quite clear. However, you may have to type too many "end of code block". I propose (optional) indented blocks as a solution

Problem

Ruby syntax is quite clear. However, you may have to type too many "end of code block". Example: if ... then
   if ... then
                if ... then
                        ...
                end            |
        end                    |  <--- 3 lines to indicate end of code block
end |

Proposal

I propose adding optional indented blocks to ruby. Indented blocks come right after a ':'. If you don't use them there is no problem. Example: An indented ruby code: def f
        if 5 > 4:
                if 5 > 2:                          <--- indentation is compulsory
                       puts "hello"

The other syntax is still compatible: def f()

        if 5 > 4
                if 5 > 2
                        puts "hello"               <--- indentation is optional
                end
        end
end

Analysis

I think that most of the current programs may remain untouched since it is not common to use ':'. Compatibility is assured.

In the future the "end of block" syntax may get deprecated and replaced by indented blocks.

Languages like Python and Haskell (see the 'do' syntax) use indented blocks to avoid this. For the above example: if ... :

   if .... :
                if ... :
                        ...

In this example ':' means "beginning of indented block" We don't have to write the three ends as they are put automatically by the lexer. As a restriction, each of the inner blocks have to be indented. The same may be applied to any definition (class, modules, methods): class c:

   ...
        ...

Most of the current programs may remain untouched since it is not common to use ':'. Compatibility is assured.

In the future the "end of block" syntax may get deprecated and replaced by indented blocks and we may be able to write: if 5 > 4 <-- the ':' is no more required

   puts "hello"

Implementation

It's a change to the lexer (waiting for responses to try an implementation).
ruby picture
Comments Current voting
Sorry, I don't want Ruby to become Another Python. --Austin Ziegler


We don't need one of Python's worst features.


As you can see, many Rubyists are not Pythonists for this very reason. A strange world :-)


A python feature that I dearly miss in ruby.


Compatibility is not 100% - this breaks:

if foo: bar end


Strongly opposed 13
Opposed 3
Neutral 2
In favor 1
Strongly advocate 1
ruby picture
If you have registered at RCRchive, you may now sign in below. If you have not registered, you may sign up for a username and password. Registering enables you to submit new RCRs, and vote and leave comments on existing RCRs.
Your username:
Your password:

ruby picture

Powered by .