Different content of Symbol.all_symbols between irb and ruby

Hi all,

I’m experimenting with symbols and how they get added to
Symbol.all_symbols. I wrote the following ruby script (saved as
symbol_table.rb):

==== begin snippet ====
puts Symbol.all_symbols.size

colors = [:red, :green, :blue]
puts Symbol.all_symbols.size

rgb_values = {:red => 255, :green => 10, :blue => 40}
puts Symbol.all_symbols.size
==== end snippet ====

If I run this using “irb symbol_table.rb” the number of entries
increases by 4 and then 1 as expected. However when I run it using “ruby
symbol_table.rb” it number of entries is the same in all three
printouts.

I’m assuming that the difference is that “ruby” parses the file prior to
execution, and thus has already inserted all the symbols into the
Symbol.all_symbols array. Can anyone confirm this or explain why I’m
wrong?

If it matters I’m running:

  • ruby 1.8.7 (2010-01-10 patchlevel 249) [i486-linux]
  • irb 0.9.5(05/04/13)

Best regards,
Einar

2011/2/27 Einar Jónsson [email protected]:

I’m assuming that the difference is that “ruby” parses the file prior to
execution, and thus has already inserted all the symbols into the
Symbol.all_symbols array. Can anyone confirm this or explain why I’m wrong?

I think so. lso try adding this:

new_symbol = “hello”.to_sym
puts Symbol.all_symbols.size

Yo will see that in irb those are two symbols more (as irb seems to
internally use symbols for new variables) and one symbol more in Ruby
script.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs