Using Ruby Symbols

前端 未结 5 990
无人及你
无人及你 2020-12-02 23:56

First time I tried learning Ruby was 2 years ago, now I have started again. The reason I stopped was because I could not understand the Symbol class. And now I am at the sam

5条回答
  •  悲哀的现实
    2020-12-03 00:24

    After ruby version 2.2 symbol GC was removed, so now mortal symbols i.e when we convert string to symbol ("mortal".to_sym) gets cleaned up from memory.

    check this out:

    require 'objspace'
    ObjectSpace.count_symbols
    {
      :mortal_dynamic_symbol=>3,
      :immortal_dynamic_symbol=>5,
      :immortal_static_symbol=>3663,
      :immortal_symbol=>3668
    }
    

    source: https://www.rubyguides.com/2018/02/ruby-symbols/

提交回复
热议问题