我的问题基于这个问题:Ruby Koan: Constants become symbols .我有以下代码:
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
正确答案应该是下面的吗?
assert_equal true, all_symbols.include?("RubyConstant".to_sym)
我知道我不应该这样做:
assert_equal true, all_symbols.include?(:RubyConstant)
因为那时我可以把任何东西放在那里,它仍然是真的
assert_equal true, all_symbols.include?(:DoesNotMatter)
提前为问一个简单的“是或否”问题而道歉。我很想知道“正确”的答案是什么。我更愿意在我上面提到的上一篇文章的评论中问这个问题,但我不能不单独发表一篇文章。
最佳答案
这是我得到的:
in_ruby_version("mri") do
RubyConstant = "What is the sound of one hand clapping?"
def test_constants_become_symbols
all_symbols_as_strings = Symbol.all_symbols.map { |x| x.to_s }
assert_equal true, all_symbols_as_strings.include?("RubyConstant")
end
end
关于Ruby Koans #75 test_constants_become_symbols,正确答案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13295776/