Ruby What do Rubyists like about Ruby?

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

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.


共收到 7 条回复

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

#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的更好)。

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