Forum: Ruby When to use : before variables?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
89dc62dd0a40f43a75d73e8059d61a91?d=identicon&s=25 daniel (Guest)
on 2005-12-04 18:50
(Received via mailing list)
Hi out there,

after looking at rails and the pickaxe book (not totally red yet, just
used as a reference) I still wondering when to use just a construct

:a => something

What does it stands for and when to use it? Please apologize if it's a
basic questions, but I can't realize myself the usage.

If someone has a page no. in the pickaxe book, I'm glad to read the
chapter ;)

Thanks in advance.
4b174722d1b1a4bbd9672e1ab50c30a9?d=identicon&s=25 leavengood (Guest)
on 2005-12-04 18:54
(Received via mailing list)
Look up Symbols. The colon (:) is used to create them.

The basic idea is that if you are using a String as the key or value
for a hash in many places, it is more efficient to use a Symbol.

Ryan
Aa1f23332dbb5408a55ed190648ba172?d=identicon&s=25 mark.ericson (Guest)
on 2005-12-04 19:06
(Received via mailing list)
It helps not to think of :a as a variable (you won't see it on
left-hand-side).  It is an identifier called a  'symbol' or in some
other
languages an 'atom' or 'interning.' A symbol is guranteed to be the same
(for the same string) everywhere it is used. Atoms can be introduced
ad-hoc
in your code and anywhere you use the same atom is assured to match
:foo !=
:bar  but :foobar == :foobar. It is a space and time saver to use
symbols as
constants, keys etc. rather than using a string constant.

More on Pg. 308 of Pickaxe 2nd edition.

<Mark/>
89dc62dd0a40f43a75d73e8059d61a91?d=identicon&s=25 daniel (Guest)
on 2005-12-04 20:03
(Received via mailing list)
Hi Mark,

Mark Ericson wrote:
> More on Pg. 308 of Pickaxe 2nd edition.

Thanks a lot. That are these kind of programming constructs I've to
learn. I'll read the pages.

g,
This topic is locked and can not be replied to.