Question about symbols

J

Jacek Olszak

Hi everyone...

I've just read the Pragmatic Programming Ruby book. But I don't understan=
d =20
one thing : why should I use symbols (colon sign before the variable name=
) =20
ie. :name ?

Regards
Jacek
http://jacekolszak.blogspot.com
 
R

Robert Klemme

K

Kirk Haines

This raises a couple of quick questions: firstly, how big is a
"limited set", in a hand-waving, give or take an order-of-magnitude
sort of estimate?

Lots.

It all depends on how much RAM you have available.

-----
a = 'a'
1.upto(2000000) do
a.to_sym
a.next!
end

puts "#{a} -- sleeping now"
sleep 30
-----

Run that. It creates two million symbols from :a to :ditoc, then sleeps so
that you can go look at memory usage before it exits.

On my box a ps shows something like this:

herbie 22539 74.4 6.3 131944 130320 pts/1 S 06:21 0:07 ruby /tmp/a.rb

It shows no significant variation between Ruby 1.8.0, 1.8.1, 1.8.2, or 1.8.3.

So, if you have the RAM, you can use a considerable number of symbols.
Secondly, I note that the FAQ says that fork "is slow". Am I right
to think that's simply the overhead of starting up the thread, and
then things run smoothly thereafter, or is there some other penalty
of which I'm not aware?

The expense of a fork is in the overhead required to create a new process.
That expense disappears into the background noise if the process that is
being forked persists for more than a short period of time, though.

If you have multiple processes that need to interact, then you have to deal
with interprocess communication. There are many ways to do this, but one
very convenient way with Ruby is to make use of Drb + Rinda + Tuplespace.
They provide a simple to setup and effective way of passing messages around.


Kirk Haines
 
D

David A. Black

Hi --

Hi everyone...

I've just read the Pragmatic Programming Ruby book. But I don't understand
one thing : why should I use symbols (colon sign before the variable name)
ie. :name ?

See the other answers for details and interesting stuff.... I just
wanted to clarify the point that you're not putting the colon before a
variable name, but before the string of which this is the equivalent
symbol. You can even do:

:"This is a symbol with whitespace"

or even the String#intern method:

"So is this".intern


David
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top