Ruby Koans #75 test_constants_become_symbols,正确答案?

标签 ruby symbols

我的问题基于这个问题: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/

相关文章:

ruby - 重新请求错误: DirtyExit - Child process received unhandled signal

ruby - Ruby如何解决 'diamond' mixin中的方法查找?

ruby-on-rails - 错误地删除了 Action 调度,如何正确取回它以便我可以正确加载我的本地主机服务器?

debugging - Vs2017 源不可用

php - 如何在php中转义字符串中的哈希符号

css - SVG - 访问 xlink :href external source 上的单个节点

不断进步的Ruby Case Statement

javascript - OpalRB:使用函数作为参数

java - 如何将此方法与另一种方法结合使用?

c++ - 模板类–找不到​​符号