Different content of Symbol.all_symbols between irb and ruby

E

Einar Jónsson

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
 
I

Iñaki Baz Castillo

2011/2/27 Einar J=C3=B3nsson said:
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 wron=
g?

I think so. lso try adding this:

new_symbol =3D "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.

--=20
I=C3=B1aki Baz Castillo
<[email protected]>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,538
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top