Ruby Koan: Constants become symbols

后端 未结 2 1682
醉酒成梦
醉酒成梦 2020-12-15 10:20

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 cla         


        
相关标签:
2条回答
  • 2020-12-15 10:30

    I'm not a Ruby guru but it looks like interpreter created this symbols during def expression evaluation. That's why these symbols are already there when you call Symbol.all_symbols. Third assert fails with second one being commented out because "string".to_sym creates symbol during methods execution i.e. after you got available symbols with all_symbols = Symbol.all_symbols.

    0 讨论(0)
  • 2020-12-15 10:34

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

    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.

    If you comment out this one:

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

    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)
    

    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.

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

    0 讨论(0)
提交回复
热议问题