本文介绍了什么是 %w “东西"?在红宝石?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我指的是 %w 运算符/构造函数/不管你怎么称呼它,像这样使用:

I'm referring to the %w operator/constructor/whatever you may call it, used like this:

%w{ foo bar baz }
=> ["foo", "bar", "baz"]

我有几个问题:

  • 那个%w东西"的正确名称是什么?操作员?文字?构造函数?
  • 我使用 {}s 而不是 []s 有关系吗?
  • 有没有其他类似的东西(例如,一个给你一个符号数组的东西)?
  • 它们可以嵌套吗(一个 %w 在另一个 %w 中,以创建嵌套数组)?
  • 在哪里可以找到有关它的文档?
  • What is the proper name of that %w "thing"? Operator? Literal? Constructor?
  • Does it matter whether I use {}s instead of []s?
  • Are there any other things like this (for example, one that gives you an array of symbols instead)?
  • Can they be nested (one %w inside another %w, in order to create nested arrays)?
  • Where can I find documentation about it?

推荐答案

不确定官方"文档,但这很不错:http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Literals#The_.25_Notation

Unsure about the "official" documentation but this is pretty good : http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Literals#The_.25_Notation

无论您使用 {} [] () 还是 <> 都没有关系,除非您的字符串包含此字符,例如:

Whether you use {} [] () or <> does not matter except if your string contains this character e.g.:

%q{a closing parenthesis: ")"}

语法非常复杂,因此记住每个变体并不是很有用,但是当您快速进行黑客攻击并希望避免手动处理转义字符时,它会派上用场.

The syntax is pretty complex so remembering every variant is not very useful, but it can come in handy when you are hacking quickly and want to avoid taking care of escape characters manually.

这篇关于什么是 %w “东西"?在红宝石?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 16:19