Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any method to write Ruby without adding "end"? [closed]

Tags:

syntax

ruby

Ruby is a beatifull language, but with a key word "end" which I hates to write many many times. Is there any method by which I can write concise code without writing "end" every time?

like image 445
jiyinyiyong Avatar asked Dec 25 '11 03:12

jiyinyiyong


2 Answers

Ok, this is partially non-responsive, but originally, begin ... end was the paradigm. And by originally, I mean languages you or I have never seen; things called funny names like Algol. Some languages (FORTRAN, Basic) staggered along for years with only single-statement conditionals.

Then C, and later, Java, came along and took over the world. They had { .. }. and that was not bad.

Python and a few others (including a microcode assembler I wrote years before Python was invented) have experimented with using indent for block structure. Nice solution but apparently it wasn't all that popular.

All of those worked but there were various issues.

Believe it or not, Ruby's syntax design no-doubt involved consideration of all these other less-than-perfect dead ends.

My suggestion is: give it another chance just the way it is.

Ruby merges the groundbreaking and technically worshipped Smalltalk and Lisp with the practical and actually useful Perl. For whatever reason, Smalltalk and (((Lisp))) haven't succeeded in 30 and 55 years, roughly, and Perl is fading. Ruby is by far the most advanced language ever to become popular.

The future is Ruby (and Python and JavaScript) and people like Ruby for a reason. One of those reasons is the really user-friendly syntax.

Believe me, the alternatives are worse.

Keep trying!

like image 157
DigitalRoss Avatar answered Nov 01 '22 01:11

DigitalRoss


You don't actually have to write end at all to write Ruby:

Foo = Class.new {
  define_method(:bar) {
      puts "I don't recommend this"; \
      return 42 \
    if (:what_is_going_on?)
  }
}

Foo.new.bar # => 42

Expect other Rubyists reading your code to wonder what got through your mind, though...

Another possibility without any end that replaces all the dreadful end with your favorite character:

# encoding: utf-8

define_singleton_method(:_){|code|
  eval(code.gsub("◊", "e\156d"))
}

_ <<-EOC
  class Foo
    def bar
      if :what_is_going_on?
        puts "I don't recommend this either"
        return 42
      ◊
    ◊
  ◊
  p Foo.new.bar  # => 42
EOC

Or replace _ with your own parser that generates ends according to the indentation!

like image 38
Marc-André Lafortune Avatar answered Nov 01 '22 00:11

Marc-André Lafortune