Ruby Koan:常量成为符号 [英] Ruby Koan: Constants become symbols

查看:52
本文介绍了Ruby Koan:常量成为符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 about_symbols.rb Ruby Koan (https://github.com/edgecase/ruby_koans) 中,我有以下代码:

In the about_symbols.rb Ruby Koan (https://github.com/edgecase/ruby_koans), I have the following code:

    RubyConstant = "What is the sound of one hand clapping?"
    def test_constants_become_symbols
      all_symbols = Symbol.all_symbols

      assert_equal true, all_symbols.include?(:"nonexistent")

      assert_equal true, all_symbols.include?(:"What is the sound of one hand clapping?")
      assert_equal true, all_symbols.include?("What is the sound of one hand clapping?".to_sym)
    end

照原样,测试通过了.

三个问题:

  1. 为什么第一个断言通过?:"nonexistent" 不应该包含在 all_symbols 中,但它包含在内,所以我一定是误解了一些东西.

  1. Why does the first assert pass? :"nonexistent" should not be included in all_symbols but it is included so I must be misunderstanding something.

当我注释掉第二个断言时,测试失败,因为 一只手拍手的声音是什么?".to_sym 未包含在 all_symbols 中,而 :What是一只手拍手的声音吗?" 包括在内.既然它们是等价的,为什么最后一个断言会失败?另外,为什么当第二个断言没有被注释掉时它会通过?(为什么第二个断言对第三个断言有影响?)

When I comment out the second assert, the test fails because "What is the sound of one hand clapping?".to_sym is not included in all_symbols whereas :"What is the sound of one hand clapping?" is included. Since they are equivalent, why does the last assert fail? Also, why does it pass when the second assert is not commented out? (Why does the second assert have any effect on the third assert?)

据我所知,这个 Ruby Koan 的目的是证明常量变成了符号(至少,这是我从方法名称中推断出来的).既然 RubyConstant 是一个常量,其值为 一只手拍手的声音是什么?",为什么 一只手拍手的声音是什么?".to_sym> 包含在符号列表中?我能想到的唯一解释是,与方法名称相反,常量实际上并没有变成符号.

To my knowledge, the point of this Ruby Koan was to demonstrate that constants become symbols (at least, that's what I'm inferring from the method name). Since RubyConstant is a constant with the value "What is the sound of one hand clapping?", why isn't "What is the sound of one hand clapping?".to_sym included in the list of symbols? The only explanation that I can think of is that, contrary to the method name, constants do not in fact become symbols.

感谢您的帮助!

推荐答案

hoha 说得对,但我会尝试扩展和澄清一下.

hoha has it right but I'll try to expand and clarify a bit.

解释器在解析test_constants_become_symbols 时会创建:nonexistent 符号.然后,当您运行它时,会调用 Symbol.all_symbols 以获取所有已知符号的列表,并且 :nonexistent 位于列表中.另请注意,"nonexistent" 上的双引号是语法问题而不是内部表示问题,因此 :nonexistent:"nonexistent" 是同样的事情.

The interpreter will create the :nonexistent symbol when it parses test_constants_become_symbols. Then, when you run it, Symbol.all_symbols is called to get a list of all known symbols and :nonexistent is in the list. Also note that the double quotes on "nonexistent" are a syntax issue rather than an internal representation issue so :nonexistent and :"nonexistent" are the same thing.

如果你注释掉这个:

  assert_equal true, all_symbols.include?(:"What is the sound of one hand clapping?")

那么 :一只手拍手的声音是什么?" 符号将不会被解析器看到,因此它不会出现在 all_symbols 数组中.当test_constants_become_symbols被执行时,下一行的.to_sym方法调用被执行;因此,:一只手拍手的声音是什么?" 符号是在您获得 all_symbols 后创建的,这将失败:

then the :"What is the sound of one hand clapping?" symbol will not be seen by the parser and so it won't be in the all_symbols array. The .to_sym method call on the following line is executed when test_constants_become_symbols is executed; so, the :"What is the sound of one hand clapping?" symbol is created after you get your all_symbols and this will fail:

  assert_equal true, all_symbols.include?("What is the sound of one hand clapping?".to_sym)

如果您在同一个解释器实例中再次执行 test_constants_become_symbols(第二个 assert_equal 仍然被注释掉),那么两个未注释的 assert_equal 调用都会通过由于第一次运行 test_constants_become_symbols 将创建 :"什么是一只手鼓掌的声音?" 和第二个 Symbol.all_symbols 将包括它在返回的数组中.

If you execute test_constants_become_symbols again in the same interpreter instance (with the second assert_equal still commented out) then both uncommented assert_equal calls will pass as the first run through test_constants_become_symbols will create the :"What is the sound of one hand clapping?" and the second Symbol.all_symbols will include it in the returned array.

irb 中运行您的代码而不将其包装在 def 中可能会帮助您了解发生了什么.

Running your code in irb without wrapping it in a def might help you see what's going on.

这篇关于Ruby Koan:常量成为符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆