为什么赞成使用%w而不是['foo','bar']

时间:2016-03-22 18:10:36

标签: ruby coding-style rubocop

我通过rubocop运行我的代码。它有一条规则:%w or %W for an array of words.

%w(some words here)   
['some', 'words', 'here']

为什么%w优先于['some', 'words', 'here']?两者似乎都是平等的。它只是减少打字,还是我们为了混淆而试图变得神秘?

4 个答案:

答案 0 :(得分:6)

有三个原因:

  1. %w是更惯用的红宝石
  2. 在阅读ruby代码时,在处理字符串数组时会遇到%w。虽然你可以写一个简单的[]并实现相同的东西,但是%w是'红宝石方式'。

    1. %w比[]
    2. 传达的意义更多

      第二个,我认为更重要的原因是%w传达了更具体的含义。使用%w创建的数组只能保存字符串,例如它不能保存整数。因此,代码通过指出此数组将只有字符串来向程序员显示其更清晰的意图。

      1. Ruby哲学:多种方式做一件事。
      2. Ruby有一种哲学(正确或错误地),语言应该给程序员多个选项来做同样的事情,这样他们就可以做他们认为正确的事情。所以,如果你不同意那个规则,那就好了:)以下是对Ruby创建者Yukihiro Matsumoto采访的片段

          

        Ruby继承了Perl的理念,即有多种方法可以做同样的事情。我从拉里沃尔那里继承了这个哲学,实际上他是我的英雄。我想让Ruby用户免费。我想给他们自由选择。人与众不同。人们选择不同的标准。但如果在许多替代方案中有更好的方法,我想通过让它变得舒适来鼓励这种方式。这就是我试图做的事情。也许Python代码更具可读性。每个人都可以编写相同风格的Python代码,因此可能更容易阅读。但是,从一个人到另一个人之间的区别是如此之大,我认为即使你使用Python,也只提供一种方法。如果可能的话,我宁愿提供很多方法,但如果可能的话,鼓励或引导用户选择更好的方式。 - Yukihiro Matsumoto 2003 http://www.artima.com/intv/rubyP.html

答案 1 :(得分:6)

它只是"神秘的"如果您不熟悉快捷方式。

我从未相信它值得强制执行,但你可以关闭这个特别的警告,如果这就是你的滚动方式而不用担心。< / p>

语法实际上更清晰了:

%w(some words here)

答案 2 :(得分:3)

是的,这是(部分原因)减少打字。但不仅如此。减少打字通常意味着阅读量减少它更容易阅读。它不是想要神秘,而是相反。此外,随着阵列变长,使用%w的优势和诱惑力变得更大,并且根据阵列的长度使用不同的符号会更加神秘。

答案 3 :(得分:2)

其实是:

// obtain the shutdown reason System.Web.ApplicationShutdownReason shutdownReason = System.Web.Hosting.HostingEnvironment.ShutdownReason; string shutdownDetail = ""; //Evaluate which option caused the error switch (shutdownReason) { case ApplicationShutdownReason.BinDirChangeOrDirectoryRename: shutdownDetail = "A change was made to the bin directory or the directory was renamed"; break; case ApplicationShutdownReason.BrowsersDirChangeOrDirectoryRename: shutdownDetail = "A change was made to the App_browsers folder or the files contained in it"; break; case ApplicationShutdownReason.ChangeInGlobalAsax: shutdownDetail = "A change was made in the global.asax file"; break; case ApplicationShutdownReason.ChangeInSecurityPolicyFile: shutdownDetail = "A change was made in the code access security policy file"; break; case ApplicationShutdownReason.CodeDirChangeOrDirectoryRename: shutdownDetail = "A change was made in the App_Code folder or the files contained in it"; break; case ApplicationShutdownReason.ConfigurationChange: shutdownDetail = "A change was made to the application level configuration"; break; case ApplicationShutdownReason.HostingEnvironment: shutdownDetail = "The hosting environment shut down the application"; break; case ApplicationShutdownReason.HttpRuntimeClose: shutdownDetail = "A call to Close() was requested"; break; case ApplicationShutdownReason.IdleTimeout: shutdownDetail = "The idle time limit was reached"; break; case ApplicationShutdownReason.InitializationError: shutdownDetail = "An error in the initialization of the AppDomain"; break; case ApplicationShutdownReason.MaxRecompilationsReached: shutdownDetail = "The maximum number of dynamic recompiles of a resource limit was reached"; break; case ApplicationShutdownReason.PhysicalApplicationPathChanged: shutdownDetail = "A change was made to the physical path to the application"; break; case ApplicationShutdownReason.ResourcesDirChangeOrDirectoryRename: shutdownDetail = "A change was made to the App_GlobalResources foldr or the files contained within it"; break; case ApplicationShutdownReason.UnloadAppDomainCalled: shutdownDetail = "A call to UnloadAppDomain() was completed"; break; default: shutdownDetail = "Unknown shutdown reason"; break; }

没有逗号。你可以使用任何匹配的“(”,“[”,“{”,“&lt;”来包含它们。

它与括号中的方法定义的可选项相同。更好的可读性。只要你知道它是什么,就不要神秘。

有关详细信息,请参阅Ruby Docs