有什么方法可以在不添加“end”的情况下编写Ruby?

发布于 2024-12-22 16:46:31 字数 73 浏览 0 评论 0原文

Ruby 是一种美丽的语言,但有一个我讨厌多次写的关键词“结束”。 有没有什么方法可以让我写出简洁的代码而不需要每次都写“end”?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

煮酒 2024-12-29 16:46:32

好吧,这部分是无响应的,但最初,begin ... end 是范例。 最初,我指的是你或我从未见过的语言;有些语言(FORTRAN、Basic)多年来一直只用单语句条件句,摇摇晃晃地发展着。

然后 C 和 Java 出现并占领了世界。他们有{..}.,这还不错。

Python 和其他一些语言(包括我在 Python 发明前几年编写的微代码汇编器)已经尝试过使用缩进来实现块结构。不错的解决方案,但显然它并不那么受欢迎。

所有这些都有效,但也存在各种问题。

不管你相信与否,Ruby 的语法设计无疑涉及到所有这些其他不太完美的死胡同的考虑。

我的建议是:再给一次机会就这样吧。

Ruby 将开创性的、技术上备受推崇的 SmalltalkLisp 与实用且有用的 Perl 结合在一起。 无论出于何种原因,Smalltalk 和 (((Lisp) )) 大约有 30 年和 55 年没有成功,Perl 正在衰落。 Ruby 是迄今为止流行的最先进的语言。

未来是 Ruby(以及 Python 和 JavaScript),人们喜欢 Ruby 是有原因的。原因之一是真正用户友好的语法。

相信我,其他选择更糟糕。

继续努力!

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!

谎言月老 2024-12-29 16:46:32

实际上,您根本不必编写 end 来编写 Ruby:

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

Foo.new.bar # => 42

不过,希望其他 Rubyists 阅读您的代码,想知道您的想法是什么......

另一种没有任何 end 的可能性 code> 用您最喜欢的字符替换所有可怕的 end

# 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

或者用您自己的解析器替换 _,该解析器根据缩进生成 end

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文