Ruby What do Rubyists like about Ruby?

hooopo · 2012年04月28日 · 最后由 ekim_sgra 回复于 2012年04月28日 · 2789 次阅读

http://www.unlimitednovelty.com/2009/03/what-do-rubyists-like-about-ruby.html

I put together a totally unscientific gimpy little web poll and posted it to the ruby-talk mailing list, asking Rubyists what their favorite Ruby features are. I received a little over 60 responses. Here are the top 10 (at the time this was posted):

Blocks (71%)
Lambdas / Procs (57%)
Mix-ins / Modules (55%)
Open classes / monkeypatching (53%)
Duck-typing (53%)
First-class regexps (49%)
Metaprogramming (49%)
Syntactic sugar e.g. val ||= x; array <<>
Optional parens / English-like readability (44%)
Object#method_missing (42%)

There's no real surprises here. Rubyists love their blocks, but perhaps even more than that, Rubyists love closures. Talking to some other people about lambdas / procs being so high, the general sentiment was that they were a feature which was used somewhat infrequently but was very handy when needed.

The mixin approach to inheritance is also quite popular. This lies alongside open classes and monkeypatching. Rubyists really seem to enjoy the ability to manipulate code inside the runtime environment, and we also see metaprogramming not too far down the list.

First class regexps have always been one of my favorites and I was glad to see they fared well. Beyond that, the type system, syntactic sugar, and method_missing were also popular.


First-class regexps 是什么,怎么我对这个没什么印象

匿名 #2 2012年04月28日

#1 楼 @HungYuHei 应该是指内建的正则表达式支持吧

#1 楼 @HungYuHei First-class regexp:

/ruby/.class # => Regexp

而其他语言,比如 Python 或者 Java,你需要 import re 或者 java.util.regex 然后再做 complie/search 等操作,正则表达式并不是这些语言的“一等公民”。

@jjym @quakewang 原来是这个意思,感谢

array <<> 是指是什么?

Blocks (71%) Lambdas / Procs (57%) Mix-ins / Modules (55%) Open classes / monkeypatching (53%) Duck-typing (53%) First-class regexps (49%) Metaprogramming (49%) Syntactic sugar e.g. val ||= x; array <<> Optional parens / English-like readability (44%) Object#method_missing (42%)

这个排序一点都不奇怪。

  • 排名第一的 block 可以说是 Ruby 独有的,独一无二的。没有它,我估计就没有 ruby.
  • 排名第二的特性,类似于 JavaScript 的动态语言,也具备,当然他们都是来自于 lisp.
  • 排名第三还是来自于 Lisp, 只不过在 Ruby 当中更加发扬广大。
  • 排名第四,我不知道是否其他语言有这个特性,不过能够运行时动态改变,真的很方便!
  • 排名第五,貌似是个动态语言就支持吧,这没啥,代表作应该是 python, ruby 只是借鉴。
  • 排名第六,绝对是 Ruby 的一大特色,你可以把 Perl 扔掉了 (虽然 Ruby 继承自 Perl, 但青出于蓝胜于蓝啊)
  • 排名第七,第十,其实就是排名第四的 Open class 的一部分。
  • 第八,第九只是因为语言自身的风格决定的。

对我来说,ruby 与其它语言与众不同的只有 Blocks 和 Open classes / monkeypatching(javascript 也有,但 ruby 的更好)。

需要 登录 后方可回复, 如果你还没有账号请 注册新账号