我的问题建立在这个问题的基础上:Ruby Koan:常量成为符号.我有以下代码:
in_ruby_version("mri") do
RubyConstant = "What is the sound of one hand clapping?"
def test_constants_become_symbols
all_symbols = Symbol.all_symbols
assert_equal __, all_symbols.include?(__)
end
end
Run Code Online (Sandbox Code Playgroud)
应该是正确的答案如下吗?
assert_equal true, all_symbols.include?("RubyConstant".to_sym)
Run Code Online (Sandbox Code Playgroud)
我知道我不应该这样做:
assert_equal true, all_symbols.include?(:RubyConstant)
Run Code Online (Sandbox Code Playgroud)
因为那时我可以把任何东西放在那里,它仍然是真的
assert_equal true, all_symbols.include?(:DoesNotMatter)
Run Code Online (Sandbox Code Playgroud)
提前道歉,问一个简单的"是或否"问题.我很好奇知道"正确"的答案是什么.我本来希望在上面提到的上一篇文章的评论中提出这个问题,但我不能不另外发表一篇文章.
Koan代码,编号75:
in_ruby_version("mri") do
RubyConstant = "What is the sound of one hand clapping?"
def test_constants_become_symbols
all_symbols = Symbol.all_symbols
assert_equal __, all_symbols.include?(__)
end
end
Run Code Online (Sandbox Code Playgroud)
我对它有点困惑,因为我刚刚发现任何针对"all_symbols.include?(__)"进行测试的符号都将匹配"true".例如,以下所有内容都应该有效:
assert_equal true, all_symbols.include?(:RubyConstant)
assert_equal true, all_symbols.include?(:"What is the sound of one hand clapping?")
assert_equal true, all_symbols.include?(:AnythingElseYouCouldWriteHere)
Run Code Online (Sandbox Code Playgroud)
用"constants_become_symbols"来学习什么?