Ruby:%w性能

cod*_*nny 2 ruby arrays performance

也许是一个简单的问题,但我们正在讨论是否更好地使用这个片段:

if %w(production staging).include?(Rails.env)
Run Code Online (Sandbox Code Playgroud)

if ["production","staging"].include?(Rails.env)
Run Code Online (Sandbox Code Playgroud)

我们只想了解哪种方法性能最佳,忽略了Ruby的sytax构造.从我在网络上可以看到,%w文字似乎是提供的空白字符串上的string.split的简写.

但哪一个实际上最快?

ps:答案的来源将不胜感激.

Mic*_*ohl 7

这是什么%w%W做,直接取自parse.y(与ommissions):

case '%':
[snip]
  switch (c) {
    [snip]
    case 'W':
      lex_strterm = NEW_STRTERM(str_dword, term, paren);
      do {c = nextc();} while (ISSPACE(c));
      pushback(c);
      return tWORDS_BEG;

    case 'w':
      lex_strterm = NEW_STRTERM(str_sword, term, paren);
      do {c = nextc();} while (ISSPACE(c));
      pushback(c);
      return tQWORDS_BEG;
Run Code Online (Sandbox Code Playgroud)

考虑到它是在解析器级别上实现的,我不会过分担心性能.