Issue #5054 has been updated by Ralph Corderoy.


Yasushi ANDO wrote:
> Ralph Corderoy wrote:
> > My suggestion is to introduce end{if,while,def,...} as keywords;
>
> Really great idea, but I'd like to avoid 'endif endif endif.' How
> about introducing ennnndif?

"endif endif endif" would be uncommon.  Normally a single "endwhile" or
whatever wrapped the nested if-statements would suffice.  When it
couldn't, an extra begin...endbegin could be used.

    # Current syntax.
    while a
        if b then
            c()
            if d then
                e()
                    if f then
                        g()
                        h() while i
                    end
                end
            end
        end
    end

    # Those "end"s can be replaced with a single endwhile.
    while a
        if b then
            c()
            if d then
                e()
                    if f then
                        g()
                        h() while i
    endwhile

    # Current syntax, more awkward example;  j() added.
    while a
        if b then
            c()
            if d then
                e()
                    if f then
                        g()
                        h() while i
                    end
                end
            end
            j()
        end
    end

    # Introducing a begin...end avoids the need for multiple "endif"s.
    while a
        if b then
            c()
            begin
                if d then
                    e()
                        if f then
                            g()
                            h() while i
            endbegin
            j()
    endwhile

"ennnndif" is poor because it makes the human count, and we're
inconsistent at that;  it would cause more bugs to be introduced.
Especially a sequence of characters all the same, but "en5d" isn't much
better because I'm now having to count outwards five blocks.  Make the
computer count;  they're good at it!  :-)

Kurt Stephens wrote:
>     module MyModule
>       class MyClass
>         def my_method
>           10.times do
>             if rand < 0.5
>               p :small
>             end; end; end; end; end
>
> it is similar to this common style:
>
>     (define (foo arg)
>       (if foo
>         (cons foo 'that)))

I agree the "end"s on the same line works, though I'd probably align
with the outermost block being closed, i.e. "module".  But when it's
used in Lisp you'll find programmers tap away at ")" with the editor
showing the matching "(" until they've entered enough.  They don't
particularly count how many to enter.

With the current style, I don't have to count the "end"s either.
There's one per line and I just keep outdenting until I'm in alignment
with the opening while/do/etc.  But having to enter them on one line
means I'm back to counting again, I feel.  I could do the old style and
then join all the lines with ";" but that's a bit long-winded.  :-)

If Ruby had labels, e.g. to allow "break" to break out of more than one
block, then we could label the start of the block and have an "endlabel
A" syntax to avoid all the in between "end"s, but it doesn't.
begin...endbegin suffices most of the time.

So, it seems my key point for a solution is it mustn't require the
programmer to count because he'll get it wrong some of the time and
readers won't bother to check most of the time.  So a means of pairing
up the opening of the block and the end is required.  The type of block
is often unique enough to suffice, e.g. the reader readily confirms the
"endwhile" matches the "while" with which it's aligned and there are no
"while"s started within.

----------------------------------------
Feature #5054: Compress a sequence of ends
http://redmine.ruby-lang.org/issues/5054

Author: Yasushi ANDO
Status: Assigned
Priority: Normal
Assignee: Yasushi ANDO
Category: Joke
Target version: 


Though as matz said at rubykaigi2011 ruby is a quite good language, many people hate a long sequence of `end' like this:

module MyModule
  class MyClass
    def my_method
      10.times do
        if rand < 0.5 
          p :small
        end 
      end 
    end 
  end 
end

So, I'd like to propose introducing a special keyword, en(n+)d. Using this keyword, we can rewrite the above example like this:

module MyModule
  class MyClass
    def my_method
      10.times do
        if rand < 0.5 
          p :small
        ennnnnd 

I know matz's already rejected a python-style block. He wrote:

> it works badly with
>   * tab/space mixture
>   * templates, e.g. eRuby
>   * expression with code chunk, e.g lambdas and blocks
http://www.ruby-forum.com/topic/108457

These bad things won't occur by introducing en(n+)d.

Some implementations already exists.

JRuby
- https://gist.github.com/1088363

CRuby
- http://www.atdot.net/sp/raw/kn9iol
- http://d.hatena.ne.jp/ku-ma-me/20110718/p1

Thanks for your consideration.


-- 
http://redmine.ruby-lang.org