我的问题建立在这个问题的基础上:Ruby Koan:常量变成符号 https://stackoverflow.com/questions/5358727/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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)