有什么方法可以在不添加“end”的情况下编写Ruby?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,这部分是无响应的,但最初,
begin ... end
是范例。 最初,我指的是你或我从未见过的语言;有些语言(FORTRAN、Basic)多年来一直只用单语句条件句,摇摇晃晃地发展着。然后 C 和 Java 出现并占领了世界。他们有{..}.,这还不错。
Python 和其他一些语言(包括我在 Python 发明前几年编写的微代码汇编器)已经尝试过使用缩进来实现块结构。不错的解决方案,但显然它并不那么受欢迎。
所有这些都有效,但也存在各种问题。
不管你相信与否,Ruby 的语法设计无疑涉及到所有这些其他不太完美的死胡同的考虑。
我的建议是:再给一次机会就这样吧。
Ruby 将开创性的、技术上备受推崇的
Smalltalk
和Lisp
与实用且有用的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
andLisp
with the practical and actually usefulPerl.
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!
实际上,您根本不必编写
end
来编写 Ruby:不过,希望其他 Rubyists 阅读您的代码,想知道您的想法是什么......
另一种没有任何
end
的可能性 code> 用您最喜欢的字符替换所有可怕的end
:或者用您自己的解析器替换
_
,该解析器根据缩进生成end
!You don't actually have to write
end
at all to write Ruby:Expect other Rubyists reading your code to wonder what got through your mind, though...
Another possibility without any
end
that replaces all the dreadfulend
with your favorite character:Or replace
_
with your own parser that generatesend
s according to the indentation!