-1

In Ruby a block is ended with the reserved word "end". And nested blocks need all their own end.

var_a = a
var_b = b

if var_a
  if var_b
    ... do something
  end
end

It is shorter to write: end(amount_of_ends_needed):

if var_a
  if var_b
    ... do something
end(2)

Is something like this possible in Ruby ?

Edit:

I see people are not in favor of this idea. However one argument is the use of a variable inside end(some_value).

value = query
if var_a
  if var_b
    ... do something
end(value)

And have a dynamic amount of nesting. By cutting the nested blocks that are superfluous.

I found this on another page: Ruby multiline block without do end , at the bottom of the page is a link to seamless

Python allows you to signal the end of a code block with indentation. Ruby suffers from an extremely verbose and tedious block terminator, "end". Much like Lisps end up with dozens of close-parens, Ruby files that use modules and classes heavily end up with a plethora of "ends" that just aren't necessary. Write a Ruby file, but skip all the "ends". Line up your code blocks like in Python. Then just call it 'your_file.rbe', require 'seamless', and require 'your_file'. Seamless does the rest. Should this ever see widespread use? I don't know. But it's pretty fun!

azbc
  • 399
  • 1
  • 5
  • 12

3 Answers3

3

I'm not aware of any language where this is a feature. It would, in my opinion, make for a terrible language design.

It sounds like you're coming from a python background, which is (unlike ruby) whitespace-sensitive. Arguing for that as a desirable language feature has some merit, but is not something that will ever be built into ruby at this point.


Interestingly, you're not the first person to come up with this idea. Here's an amusing (joke/rejected) ruby feature request from a few years ago:

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

Or:

 module MyModule
   class MyClass
     def my_method
       10.times do
         if rand < 0.5
           p :small
 eeeeeeeeend
         ^^^ <- same place of original "end"!

Or:

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

Or:

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

Or perhaps instead this should be "fold up" syntax:

module MyModule
  class MyClass
    def my_method
      10.times do
        if rand < 0.5 
          p :small
fuuuuu
Tom Lord
  • 27,404
  • 4
  • 50
  • 77
  • 1
    "fold up" syntax – Sergio Tulentsev Nov 28 '17 at 14:28
  • To immediately dismiss this as a "TERRIBLE LANGUAGE DESIGN" is a bit harsh, isn't it ? The design attempt is actually having it dimensionally better structured, by finding something to omit al these branches. Well, maybe I am not intelligent enough. – azbc Nov 28 '17 at 17:25
  • 2
    @azbc With respect, I did not "immediately dismiss it". I even provided you with some research on how others have considered it before, and other proposed forms of syntax. – Tom Lord Nov 28 '17 at 17:36
  • 1
    If you follow that link, you'll also find an MRI implementation of the `ennnnd` syntax. Which essentially answers your question of "yes, it is possible". (If you add it to the core language parser.) – Tom Lord Nov 28 '17 at 17:39
  • 1
    My point around "terrible design" is primarily because this is a "pointless" feature. If your code is only nested e.g. 2 layers deep, then `end(2)` doesn't help very much; meanwhile if your code is e.g. 100 layers deep then you have bigger problems! – Tom Lord Nov 28 '17 at 17:40
  • 1
    @Tom Lord - yes, sorry, that is not nice of me to say, indeed you provide me with a lot of help, thank you :) – azbc Nov 28 '17 at 18:22
1

No, That is a violation of basic ruby syntax and will fail in the parser level itself. And it is not about efficiency, you won't get any meaningful performance boost by doing that. If you meant the extra effort to write end after end of the each block, use plugin for doing that in your favourite text editor

Note, If your code is too much nested, then that is probably a code smell

Tachyons
  • 2,131
  • 1
  • 21
  • 35
  • 1
    That's a good point. Who even _writes_ `end` manually these days? My editor inserts them for me (and keeps an eye on the balancing) – Sergio Tulentsev Nov 28 '17 at 13:23
0

Would it be seriously possible to have this feature ? end(value) ?

Of course, everything is possible, given enough determination. Although, there is a reason why no single language has this. The reason being that in practice nobody writes a thousand nested ifs, so such a feature has negligible practical benefit, while being extremely costly in implementation and understanding.

For example, you suggested that putting a number not big enough in end(x) would ignore all blocks, whose nesting is larger than that. That is a language I wouldn't touch with a mile-long pole. Either my code all works or it fails to parse.

But there are languages with no end at all, where nesting is controlled by indentation. Like python or coffeescript. So if you're bothered by writing out ends and your editor doesn't help, you could try using those. You'll still have to keep a really close eye on what level of indentation you're typing the code currently. Same coin, different side.

Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367