为什么喜欢使用 %w 而不是 ['foo', 'bar']
Why favor the use of %w over ['foo', 'bar']
我 运行 我的代码通过 rubocop。它有一个规则:%w
or %W
for an array of words.
%w(some words here)
['some', 'words', 'here']
为什么 %w
比 ['some', 'words', 'here']
更受欢迎?两者似乎是平等的。只是少打字,还是我们为了混淆而试图变得神秘?
是的,(部分是因为)打字少了。但不仅如此。少打字通常意味着少阅读;它更容易阅读。它并不是要隐晦,恰恰相反。此外,随着数组变长,使用%w
的优势和诱惑也越来越大,根据数组的长度使用不同的表示法是比较隐晦的。
三个原因:
- %w 更加地道ruby
阅读 ruby 代码时,您会在处理字符串数组时经常遇到 %w。虽然你可以写一个简单的 [] 并实现同样的事情 %w 是 'ruby way'.
- %w 比 []
表达的意思更多
第二个也是我认为更重要的原因是%w 传达了更具体的含义。用 %w 创建的数组只能保存字符串,例如不能保存整数。所以代码通过指出这个数组将永远只有字符串来向程序员更清楚地展示它的意图。
- Ruby 哲学:用多种方法做一件事。
Ruby 有一种哲学(正确或错误),即语言应该为程序员提供多种选择来做同样的事情,这样他们就可以做他们认为正确的事情。因此,如果您不同意规则也没关系 :) 这是对 Ruby Yukihiro Matsumoto
的创建者的采访片段
Ruby inherited the Perl philosophy of having more than one way to do the same thing. I inherited that philosophy from Larry Wall, who is my hero actually. I want to make Ruby users free. I want to give them the freedom to choose. People are different. People choose different criteria. But if there is a better way among many alternatives, I want to encourage that way by making it comfortable. So that's what I've tried to do. Maybe Python code is a bit more readable. Everyone can write the same style of Python code, so it can be easier to read, maybe. But the difference from one person to the next is so big, providing only one way is little help even if you're using Python, I think. I'd rather provide many ways if it's possible, but encourage or guide users to choose a better way if it's possible. - Yukihiro Matsumoto 2003 http://www.artima.com/intv/rubyP.html
只是 "cryptic" 如果您不熟悉该快捷方式。
我一直不相信它值得强制执行,但您可以关闭此特定警告,如果您这样做的话,不用担心它。
语法实际上更清晰:
%w(some words here)
实际上是:
%w(some words here)
没有逗号。您可以使用任何匹配的“(”、“[”、“{”、“<”将它们括起来。
它与括号作为方法定义的可选行在同一行。更好的可读性。只要您知道它的用途,就不会神秘。
请参阅 Ruby Docs 了解更多信息。
我 运行 我的代码通过 rubocop。它有一个规则:%w
or %W
for an array of words.
%w(some words here)
['some', 'words', 'here']
为什么 %w
比 ['some', 'words', 'here']
更受欢迎?两者似乎是平等的。只是少打字,还是我们为了混淆而试图变得神秘?
是的,(部分是因为)打字少了。但不仅如此。少打字通常意味着少阅读;它更容易阅读。它并不是要隐晦,恰恰相反。此外,随着数组变长,使用%w
的优势和诱惑也越来越大,根据数组的长度使用不同的表示法是比较隐晦的。
三个原因:
- %w 更加地道ruby
阅读 ruby 代码时,您会在处理字符串数组时经常遇到 %w。虽然你可以写一个简单的 [] 并实现同样的事情 %w 是 'ruby way'.
- %w 比 [] 表达的意思更多
第二个也是我认为更重要的原因是%w 传达了更具体的含义。用 %w 创建的数组只能保存字符串,例如不能保存整数。所以代码通过指出这个数组将永远只有字符串来向程序员更清楚地展示它的意图。
- Ruby 哲学:用多种方法做一件事。
Ruby 有一种哲学(正确或错误),即语言应该为程序员提供多种选择来做同样的事情,这样他们就可以做他们认为正确的事情。因此,如果您不同意规则也没关系 :) 这是对 Ruby Yukihiro Matsumoto
的创建者的采访片段Ruby inherited the Perl philosophy of having more than one way to do the same thing. I inherited that philosophy from Larry Wall, who is my hero actually. I want to make Ruby users free. I want to give them the freedom to choose. People are different. People choose different criteria. But if there is a better way among many alternatives, I want to encourage that way by making it comfortable. So that's what I've tried to do. Maybe Python code is a bit more readable. Everyone can write the same style of Python code, so it can be easier to read, maybe. But the difference from one person to the next is so big, providing only one way is little help even if you're using Python, I think. I'd rather provide many ways if it's possible, but encourage or guide users to choose a better way if it's possible. - Yukihiro Matsumoto 2003 http://www.artima.com/intv/rubyP.html
只是 "cryptic" 如果您不熟悉该快捷方式。
我一直不相信它值得强制执行,但您可以关闭此特定警告,如果您这样做的话,不用担心它。
语法实际上更清晰:
%w(some words here)
实际上是:
%w(some words here)
没有逗号。您可以使用任何匹配的“(”、“[”、“{”、“<”将它们括起来。
它与括号作为方法定义的可选行在同一行。更好的可读性。只要您知道它的用途,就不会神秘。
请参阅 Ruby Docs 了解更多信息。