DHH观点:为什么我偏爱Ruby?
David Heinemeier Hansson(DHH)在RailsConf 2006的Keynote Address中讨论Rails具有各种conventions时,提到这样的观点,即任何一种paradigm都不是万能的,任何paradigm都会有例外情况,例外情况就须要例外处理;Rails在提倡其conventions的同时,仍然允许我们避开conventions,采用更原始的办法来处理同样的事情。Rails正是这种既具有明显conventions又允许例外情况的优秀语言。对此,DHH说如是:
..And that's why I personally love Ruby. Ruby has strong opinions of how you should do things, but it allows you to do basically everything if you really, really want to. A private method, it is only private until you call, well, .send(), simple for the method name. You can circumvent these things because sometimes it's DAMN useful to be able to circumvent these things, and you know the trade-offs -- you know that this special piece of code is being written in special way because it needs special consideration, it's not like the rest, and I should consider carefully the tradeoff I'm making.
……这也是我个人偏爱Ruby的原因:对于“你应该如何做事情”,Ruby持有强烈的观点,但在你真的非常非常想绕开conventions做事情的时候,Ruby也允许你做几乎所有事情。比如,private方法是私有的,但若你使用.send()进行调用,这种私有性就失效了。在Ruby中你可以绕开规则,因为有时候绕开规则真的非常受用,而且你在Ruby中绕开规则时,你会非常清楚其涉及到的利弊权衡与折中——你知道,对这一段特别的代码需要作特殊考虑,它是以特殊的方式写就的,它与惯常用法不同,你应该仔细考虑你所做的利弊权衡与折中。
# 有时候绕开规则真的非常受用
class Klass
private
def hello(*args)
puts "Hello " + args.join(' ')
end
end
k = Klass.new
# 使用 .send() 方法绕开private访问控制规则
k.send :hello, "gentle", "readers"
# 按惯常方式会受到访问控制规则的制约:
k.hello("gentle", "readers")
# Klass.rb:10: private method `hello' called for #<Klass:0x2b4b2dc> (NoMethodError)
DHH观点:为什么我偏爱Ruby?
转载于:https://www.cnblogs.com/kingofark/archive/2006/12/13/591118.html