Symbols vs. constants?

How are symbols and constant (capitalized) strings similar or
dissimilar? They both seem to be used to store text that is not intended
to change. In what situations should I use one or the other? How should
I separate them in my brain?

Hi –

On Tue, 22 Dec 2009, Sonja Elen K. wrote:

How are symbols and constant (capitalized) strings similar or
dissimilar? They both seem to be used to store text that is not intended
to change. In what situations should I use one or the other? How should
I separate them in my brain?

A constant is an identifier to which you can assign an object. It is
not, itself, an object.

X = "hi"   # X is a constant, "hi" is an object (a string)

A symbol is an object. You can’t assign to it; it’s not an l-value.

David

Constants are variables that can be assigned to any object (only once).
While symbols are immutable instances of class Symbol. Those are 2
totally different concepts.

Gennady.

On Tue, Dec 22, 2009 at 9:07 AM, Gennady B.
[email protected] wrote:

Constants are variables that can be assigned to any object (only once).

Hi,

It seems to me a constant can be changed during the runtime.

Z=“hello”
=> “hello”
Z=1234
(irb):11: warning: already initialized constant Z
=> 1234
Z
=> 1234

So does it mean, it can be changed, but not encouraged?

Regards,
Young.

On Dec 21, 2009, at 5:15 PM, Young H. wrote:

Z=1234
(irb):11: warning: already initialized constant Z
=> 1234
Z
=> 1234

So does it mean, it can be changed, but not encouraged?

You may say so. I just did not want to bring it up for simplicity sake.
For me any warning produced by my code must be fixed, and in this case
the fix would be to check that constants are assigned only once. I
personally would prefer an exception for such cases.

Gennady.

Symbols are like an identifier. They’re not really strings although
they’re
close to it, and they’re generally used instead of strings when they’re
not
changing.

Symbols aren’t re-created, that’s the main benefit of using them over
plain
old strings. When you first declare :foo every time you use :foo again,
it’s not re-created/re-instantiated… it’s just looked up and re-used.

This is good and bad. It’s good because there’s very little overhead to
using symbols compared to strings, but if you arbitrarily convert some
string into a symbol, it never leaves memory. Watch converting user
input
to a symbol!

In contrast a CONSTANT is a container for a value. The value can be any
object. This gives you a handy reference to the same instance of an
object.

MY_CONSTANT = [:one, :two, :three]

Hope that helps :slight_smile:

Cheers
Daniel

=> 1234

Z
=> 1234

So does it mean, it can be changed, but not encouraged?

In fact it is greatly discouraged. There is never, ever
a reason to change a constant once it is set. If you need
something that acts like a constant, yet can be reassigned
there is a construct for that: it’s called a variable.

Reassigning constants implicitly gives the next person to
work on the code permission to beat you about the head with
a large trout.

There are some important difference between the two. While the object
referred to by a constant will (in most cases) remain the same once
its set, the object itself might change significantly. For example:

AN_ARRAY = []

will point to that particular array, but if you add an object to that
array there can be consequences. For example, if you use it as a key
to a hash (which i don’t recommend.).

hash = {}
hash [AN_ARRAY] = true
hash << “string”
hash[AN_ARRAY] => nil

A symbol is immutable and therefore will always refer to the same
element of a hash.

Hi there,

About symbol I always have the question, is ruby’s symbol the same one
as (or similiar to) Perl’s symbol?
Thanks for pointing.

On Dec 21, 2009, at 7:38 PM, Gennady B. wrote:

For me any warning produced by my code must be fixed, and in this case the fix would be to check that constants are assigned only once.

You are a hero. I wish all Ruby library developers could hear your
wisdom.

In fact, I would support Ruby playing a low quality audio file of your
voice stating the above line every time a warning is printed! Who’s
with me?!?

:smiley:

James Edward G. II

On Mon, Dec 21, 2009 at 8:15 PM, Young H. [email protected] wrote:

Z=1234
(irb):11: warning: already initialized constant Z
=> 1234
Z
=> 1234

So does it mean, it can be changed, but not encouraged?

Yes.

You might say that Ruby is an example of what we called Finagle’s law
in engineering school back n the 70s. Finagle’s law states that “all
constants are variable!”


Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

On Dec 21, 2009, at 7:57 PM, Walton H. wrote:

=> 1234

Z
=> 1234

So does it mean, it can be changed, but not encouraged?

In fact it is greatly discouraged. There is never, ever
a reason to change a constant once it is set.

I’m not sure we should go quite THAT far. It is used in the wild for
features people do like.

There are also ways to do it without a warning.

James Edward G. II

Young H. wrote:

Hi there,

About symbol I always have the question, is ruby’s symbol the same one
as (or similiar to) Perl’s symbol?

I don’t think Perl has anything like Ruby’s symbols (except perhaps
internally).

Thanks for pointing.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

-----Original Message-----
From: James Edward G. II [mailto:[email protected]]

I’m not sure we should go quite THAT far. It is used in the wild for
features people do like.

There are also ways to do it without a warning.

Really? For what purpose? I can’t think of any instance that you’d
need to change a constant where a variable wouldn’t suffice (but of
course that doesn’t mean it doesn’t exist).

James Edward G. II wrote:

On Dec 21, 2009, at 7:38 PM, Gennady B. wrote:

For me any warning produced by my code must be fixed, and in this case the fix would be to check that constants are assigned only once.

You are a hero. I wish all Ruby library developers could hear your
wisdom.

I don’t. There are certain contexts where warnings are generated even
though the code is fine. I’m not going to waste time fixing otherwise
well-written code because someone decided that a warning should be
generated.

Warnings are just that: warnings – not errors. Sure, 99% of the time
it’s better to avoid the warning, but I’m not going to worry too much
about the other 1%.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

On Mon, Dec 21, 2009 at 8:38 PM, Gennady B.
[email protected] wrote:

So does it mean, it can be changed, but not encouraged?

You may say so. I just did not want to bring it up for simplicity sake. For me any warning produced by my code must be fixed, and in this case the fix would be to check that constants are assigned only once. I personally would prefer an exception for such cases.

so long as remove_const still would work, I think making reassignment
an exception would be a good idea. This way, you’d need to be
explicit about your decision to change what a constant points to.

-greg

On Tue, Dec 22, 2009 at 1:58 PM, Marnen Laibow-Koser [email protected]
wrote:

Young H. wrote:

Hi there,

About symbol I always have the question, is ruby’s symbol the same one
as (or similiar to) Perl’s symbol?

I don’t think Perl has anything like Ruby’s symbols (except perhaps
internally).

Perl does have something called symbol table:
http://www.sdsc.edu/~moreland/courses/IntroPerl/docs/manual/pod/perlmod.html#Symbol_Tables
Thanks.

On Dec 22, 2009, at 12:59 AM, Walton H. wrote:

course that doesn’t mean it doesn’t exist).
I think Rails uses it to reload class definitions. That is to say that
a particular constant, such as ArticleController, will refer to
different class objects as the class definition changes.

Gary W.

Young H. wrote:

On Tue, Dec 22, 2009 at 1:58 PM, Marnen Laibow-Koser [email protected]
wrote:

Young H. wrote:

Hi there,

About symbol I always have the question, is ruby’s symbol the same one
as (or similiar to) Perl’s symbol?

I don’t think Perl has anything like Ruby’s symbols (except perhaps
internally).

Perl does have something called symbol table:
http://www.sdsc.edu/~moreland/courses/IntroPerl/docs/manual/pod/perlmod.html#Symbol_Tables
Thanks.

Yes, but you rarely touch that in actual practice. Ruby symbols, OTOH,
are easily manipulated and widely used. They have a much wider range of
applications than Perl symbols.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Hi,

Am Dienstag, 22. Dez 2009, 09:57:28 +0900 schrieb Sonja Elen K.:

How are symbols and constant (capitalized) strings similar or
dissimilar? They both seem to be used to store text that is not intended
to change. In what situations should I use one or the other? How should
I separate them in my brain?

Constants are variables:

X = “hello”
X = “bye” # not allowed (just a warning)
X.replace “bye” # this is allowed!
X.freeze
X.replace “hi again” # forbidden (TypeError)

But you can also freeze other variables:

x = “foo”
x.freeze
x.replace “bar” # forbidden (TypeError)

Symbols are objects but do not have no replace' method neithersub’, gsub',tr’, `succ!’, and what else could change its
contents.

x = :foo
X = :bar
x.replace sth # forbidden (NoMethodError)

Could it be that I didn’t use `freeze’ for years? Did I miss
something?

Bertram