public
Last active

My thought on improving Ruby construct end syntax.

  • Download Gist
gistfile1.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
# I have now programmed in a lot of languages, and one thing I can say for sure
# is that shell scripting does construct end styles very well.
 
#
# Example from http://redmine.ruby-lang.org/issues/5054
#
# This is indeed also one of my few personal issues with Ruby syntax, the end trail.
#
module MyModule
class MyClass
def my_method
10.times do
if rand < 0.5
p :small
end
end
end
end
end
 
# For longer codebases, this lets you more clearly know where you stand and
# what is being ended.
module MyModule
class MyClass
def my_method
10.times do
if rand < 0.5
p :small
fi
od
fed
ssalc
eludom
 
# And allow for shortened forms,
module MyModule
class MyClass
def my_method
10.times do
if rand < 0.5
p :small
fi
od
ed #'end def'
ec # 'end class'
em # 'end module'
 
# es => 'end case'
 
#
# I *am* ok with this also :)
#
module MyModule
class MyClass
def my_method
10.times do
if rand < 0.5
p :small
 
 
# Suggested by @elliottcable:
module MyModule {
class MyClass {
def my_method {
10.times { # This one already works ;)
if rand < 0.5 {
p :small
}
}
}
}
}

I disagree with the shortened forms outright, first of all. That’s a terrible idea. Instead of two-character ones, allow for slightly shortened forms of the really long ons. dom for module, perhaps.

In either case, I’m in a similar situation: I’ve been away from Ruby for a long time, and if there’s one thing I’ve learned, it’s that Brackets Work Really, **Really* Well™. A much better improvement to Ruby’s syntax would be allowing brackets in place of every location where end is used. (I forked and threw out a fairly bad example: https://gist.github.com/1095649)

+1 on the brackets. If only for the ease of code editors being able to highlight the beginning and ends without it being very ugly.

I wish backwards ending statements a short but painful death. Sadly this crime against the programmer community is being perpetuated by several languages, and so I have to look at it at times even though it tortures me to do so.

Brackets do work nicely, especially with code folding... however they are no better than 'end' when wanting to know what the hell is being ended and aiding with keeping track of where you are in a construct nesting.

Also, IMHO if you want to go the bracket route then we might as well adopt python style syntax indentation and not even need them :)

Oh and btw @elliottcable, your solution is discredited due to using tabs instead of spaces :P ::grin::

Gotta toss a whitespace-aware version in there! Python/Coffee/Haml-style.

@wayneeseguin get yourself The Editor. Like Vim. ;)

Also, this example is somewhat a greenhouse one.

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

In other words, this is not a language problem.

I've been having this "problem" with dapper-dan, for instance:

html5 :root => true do
    head do
        title @title
        description @description
        keywords @keywords
        stylesheet 'application.css'  # Links to /asset/stylesheet/application.css
        javascript 'application.js'    # Links to /asset/javascript/application.js
    end

    body do
        header do
           render :partial => :header
        end

        # This is where Rails puts all the content from a view...
        article :id => 'content', :class => '3column' do
            yield
        end

        footer do
           render :partial => :footer
        end
    end
end

The first recourse is to partialize the code, but that only makes other problems more prominent. In my case, however, your solution does very little! I know I'm going to hate myself for suggesting it, but what if there was:

if true
  while true
    some_method do
      ...
    /some_method
  /while
/if

You might want to bring those up here.

@rkh you mean in the url I linked to at the top of the gist? :)

@cheba the example is contrived yes, I nabbed it from the discussion is all. It is only meant to exhibit an 'end trail'

@wayneeseguin You just put that there, right? :D

@rkh I'd claim that was true however the git history would prove me wrong ;)

Well shit, if this is going to go on like this:

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

Short-hand ?:

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

Very short-hand:

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

And double hell with it. Let Ruby support Python style indention if : is used.

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

Postfixing the name of the start does not solve the problem.

  1. You write twice as much in most cases, you're now writing XML plus an extra character.
  2. This only looks swell if you're writing a module, with a single class, with a single method, with a single block, with a single if condition.
  3. Still requires you to keep track of the other end, which is the bigger problem here.

And while the joke continues here's a lovely one:

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

.i .ui!
how about,
module.MyModule~class.MyClass~def.my_method~10.times.do~if(rand < 0.5)~p :small

Had to see that all hi-litey

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

Eek! It sucks :-D

Perhaps we can strip out even more characters and go the minimalistic approach! ;)

Hah. Yes, we had a long discussion about using Unicode box-drawing characters to denote blocks early-on in the development of Paws; I think we’ll have at least one patois playing with things like that in the long run.

Er, what do you mean tabs? I haven’t used a hard-tab in a fuckin’ decade! In fact, despite the shitstorm it abused, my language downright doesn’t allow hardtabs instead. #ew

This gist provides quite a lot of food-for-thought as a developer of a nascent programming language.

Now, that said, PLEASE STOP STUFFING MY INBOX WITH NOTIFICATIONS ;_;

@elliottcable I dont believe there is currently a way to 'unsubscribe' from a gist notification feed?

No, there’s not. And that’s the point of my all-caps request. ;D

HELLO GUYS HOW ARE YOU?

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.