- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
在 about_symbols.rb Ruby Koan (https://github.com/edgecase/ruby_koans) 中,我有以下代码:
RubyConstant = "What is the sound of one hand clapping?"
def test_constants_become_symbols
all_symbols = Symbol.all_symbols
assert_equal true, all_symbols.include?(:"nonexistent")
assert_equal true, all_symbols.include?(:"What is the sound of one hand clapping?")
assert_equal true, all_symbols.include?("What is the sound of one hand clapping?".to_sym)
end
照原样,测试通过。
三个问题:
为什么第一个断言通过了? :"nonexistent"
不应该包含在 all_symbols 中,但它包含在内,所以我一定是误解了什么。
当我注释掉第二个断言时,测试失败,因为 "What is the sound of one hand clapping?".to_sym
不包含在 all_symbols 而 :"What is the sound of one hand clapping?"
已经包括了。既然是等价的,为什么最后一个assert会失败呢?另外,为什么当第二个断言没有被注释掉时它通过了? (为什么第二个断言对第三个断言有影响?)
据我所知,这个 Ruby Koan 的目的是证明常量变成了符号(至少,这是我从方法名称中推断出来的)。由于 RubyConstant 是一个值为 "What is the sound of one hand clapping?"
的常量, 为什么不是 "What is the sound of one hand clapping?".to_sym
包含在符号列表中?我能想到的唯一解释是,与方法名称相反,常量实际上并没有变成符号。
感谢您的帮助!
最佳答案
hoha 说得对,但我会尝试扩展和澄清一点。
解释器在解析test_constants_become_symbols
时会创建:nonexistent
符号。然后,当您运行它时,将调用 Symbol.all_symbols
以获取所有已知符号的列表,并且 :nonexistent
位于列表中。另请注意,"nonexistent"
上的双引号是语法问题而不是内部表示问题,因此 :nonexistent
和 :"nonexistent"
是同样的事情。
如果你注释掉这个:
assert_equal true, all_symbols.include?(:"What is the sound of one hand clapping?")
那么 :"What is the sound of one hand clapping?"
符号将不会被解析器看到,因此它不会出现在 all_symbols
数组中.执行test_constants_become_symbols
时会执行下面一行的.to_sym
方法调用;所以,:"What is the sound of one hand clapping?"
符号是在你得到你的 all_symbols
之后创建的,这将失败:
assert_equal true, all_symbols.include?("What is the sound of one hand clapping?".to_sym)
如果你在同一个解释器实例中再次执行 test_constants_become_symbols
(第二个 assert_equal
仍然被注释掉)那么两个未注释的 assert_equal
调用都会通过因为第一次运行 test_constants_become_symbols
将创建 :"What is the sound of one hand clapping?"
而第二个 Symbol.all_symbols
将包括它在返回的数组中。
在 irb
中运行您的代码而不将其包装在 def
中可能有助于您了解发生了什么。
关于 ruby 公案 : Constants become symbols,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5358727/
我们的开发环境是这样配置的,当我们运行代码的调试版本时,它会在崩溃或 ^C 时进入 gdb。随着最近的一些更改,这种情况不再发生(退出程序而不是进入 gdb),我怀疑符号大小的增加导致了这个问题。 有
刚刚浏览了一个教程,想到了我看到的地方 first_name: 还有一个地方 :first_name 这样对吗?有什么区别? 最佳答案 哈希语法在 Ruby 1.9.2 中发生了变化,以更接近 jso
这里是一个相当抽象的问题,因为我不知道从哪里开始我自己的调查。 我有一个用 CMake 构建的 C 包,它生成 librpdb.so;我为同一个库设置了一个 Ruby Gem,它生成 rpdb.bun
我尝试使用 Symbol 创建对象键并用 Symbol.for 找到对应的值,但它不起作用: const sym = Symbol("x"); let obj = { [sym]: "val" }
这可能是一个愚蠢的问题,但我很高兴知道为什么我们使用带有一些标志的短形式的单符号和带有完整标志的双符号? 例子: 1) -h & --help 2) -f & --force 谁能解释一下原因? 最
我们希望能够在删除物理构建区域时删除符号服务器内容,symstore del 命令对事务 ID 起作用。这是未知的。 How to extract the transaction ID based o
我在一个我不太理解的小程序上遇到这个问题(我对节点红色有点陌生),代码是 var profile = msg.user.profile; var cart = profile.cart = pr
我正在尝试创建一种工资单以在控制台中打印,但我从代码中收到以下错误。很多时候它实际上只是一个错误,但我认为我没有足够的 java 知识来自己修复它。 import java.io.*; import
在 C# 项目中,我在 UnhandledException 中创建了小型转储。在我的 Dev 机器中,项目源和 bin 位于路径 K:\projects\*MYPROJECT* 下,如果我设法让它在
我正在尝试针对另一个使用 libcurl 共享库的共享库 (libtheirstuff.so) 交叉编译我自己的共享库 (libmystuff.so),但出现以下错误: libmystuff.so:
我试图遍历一个数组来检查它是否包含任何通过指定函数的项目。我通过向 Array 对象添加一个 .any() 原型(prototype)来做到这一点: Array.prototype.any = (co
除了这个 undefined symbol 错误外,一切正常: bash-3.2$ make g++ -Wall -g solvePlanningProblem.o Position.o AStarN
我 rsync 目录“Promotion”包含两台具有不同目录结构的机器之间的绝对符号链接(symbolic link)。因此绝对符号链接(symbolic link)在两台机器上都不起作用。为了使它
我有以下 JSX - What is your e-mail address? setStateForProperties(e)}
根据 SVG 的 symbol文档,我可以添加 refX/refY属性给它。 如果我理解正确,我可以使用这些属性来定义符号坐标系中的引用点,因此当我使用 引用它时元素,它将相对于该引用点(而不是默认
请解释以下有关“找不到符号”,“无法解析符号”或“找不到符号”错误的信息: 是什么意思? 什么原因可以导致它们? 程序员如何解决它们? 该问题旨在对Java中的这些常见编译错误进行全面的问答。 最佳答
请解释以下有关“找不到符号”,“无法解析符号”或“找不到符号”错误的信息: 是什么意思? 什么原因可以导致它们? 程序员如何解决它们? 该问题旨在对Java中的这些常见编译错误进行全面的问答。 最佳答
请解释以下有关“找不到符号”,“无法解析符号”或“找不到符号”错误的信息: 是什么意思? 什么原因可以导致它们? 程序员如何解决它们? 该问题旨在对Java中的这些常见编译错误进行全面的问答。 最佳答
请解释以下有关“找不到符号”,“无法解析符号”或“找不到符号”错误的信息: 是什么意思? 什么原因可以导致它们? 程序员如何解决它们? 该问题旨在对Java中的这些常见编译错误进行全面的问答。 最佳答
请解释以下有关“找不到符号”,“无法解析符号”或“找不到符号”错误的信息: 是什么意思? 什么原因可以导致它们? 程序员如何解决它们? 该问题旨在对Java中的这些常见编译错误进行全面的问答。 最佳答
我是一名优秀的程序员,十分优秀!