【发布时间】:2012-10-29 01:07:45
【问题描述】:
我的问题建立在这个问题之上: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)
提前向您提出简单的“是或否”问题表示歉意。我很想知道“正确”的答案是什么。我宁愿只在上面提到的上一篇文章中的 cmets 中提出这个问题,但我不能不单独发帖。
【问题讨论】:
-
只要你依赖include?只返回
true或false,而不是assert_equal,你可以只使用assert Symbol.all_symbols.include( "RubyConstant" )。 -
我认为对于这个练习,我被要求填写空白。但是,是的,
assert Symbol.all_symbols.include("RubyConstant")也应该返回true。谢谢!