Symbols vs. constants?

S

Sonja Elen Kisa

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?
 
D

David A. Black

Hi --

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

--
David A. Black
Senior Developer, Cyrus Innovation Inc.
THE COMPLEAT RUBYIST, Ruby training with Black/Brown/McAnally!
January 22-23, Tampa, Florida
Info and registration at http://www.thecompleatrubyist.com
 
G

Gennady Bystritsky

Constants are variables that can be assigned to any object (only once). Whi=
le symbols are immutable instances of class Symbol. Those are 2 totally dif=
ferent concepts.

Gennady.
 
Y

Young H.

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
=> 1234

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

Regards,
Young.
 
G

Gennady Bystritsky

=20
Hi,
=20
It seems to me a constant can be changed during the runtime.
=20
(irb):11: warning: already initialized constant Z
=3D> 1234
=3D> 1234
=20
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 woul=
d prefer an exception for such cases.

Gennady.
 
D

Daniel N

[Note: parts of this message were removed to make it a legal post.]

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 = [:eek:ne, :two, :three]

Hope that helps :)

Cheers
Daniel
 
W

Walton Hoops

-----Original Message-----
From: Young H. [mailto:[email protected]]
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
=> 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.
 
Y

Young H.

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.
 
R

Rick DeNatale

Hi,

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

(irb):11: warning: already initialized constant Z
=> 1234
=> 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
 
J

James Edward Gray II

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?!?

:D

James Edward Gray II=
 
J

James Edward Gray II

-----Original Message-----
From: Young H. [mailto:[email protected]]
Hi,
=20
It seems to me a constant can be changed during the runtime.
=20
Z=3D"hello" =3D> "hello"
Z=3D1234
(irb):11: warning: already initialized constant Z
=3D> 1234
=3D> 1234
=20
So does it mean, it can be changed, but not encouraged?
=20
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 Gray II
 
M

Marnen Laibow-Koser

Young said:
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,
 
W

Walton Hoops

-----Original Message-----
From: James Edward Gray 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).
 
M

Marnen Laibow-Koser

James said:
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,
 
G

Gregory Brown

You may say so. I just did not want to bring it up for simplicity sake. F=
or me any warning produced by my code must be fixed, and in this case the f=
ix would be to check that constants are assigned only once. I personally wo=
uld 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
 
G

Gary Wright

-----Original Message-----
From: James Edward Gray II [mailto:[email protected]]
=20
I'm not sure we should go quite THAT far. It is used in the wild for
features people do like.
=20
There are also ways to do it without a warning.
=20
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).

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 Wright=
 
B

Bertram Scharpf

Hi,

Am Dienstag, 22. Dez 2009, 09:57:28 +0900 schrieb Sonja Elen Kisa:
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 neither
`sub', `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
 
R

Rick DeNatale

Hi,

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

Constants are variables:

=A0X =3D "hello"
=A0X =3D "bye" =A0 =A0 =A0 =A0 =A0 =A0 # not allowed (just a warning)
=A0X.replace "bye" =A0 =A0 =A0 # this is allowed!
=A0X.freeze
=A0X.replace "hi again" =A0# forbidden (TypeError)

But you can also freeze other variables:

=A0x =3D "foo"
=A0x.freeze
=A0x.replace "bar" =A0 =A0 =A0 # forbidden (TypeError)

No you can't freeze variables, you can freeze the objects they reference.


ruby-1.8.7-p174 > x =3D 'foo'
=3D> "foo"
ruby-1.8.7-p174 > x.freeze
=3D> "foo"
ruby-1.8.7-p174 > x =3D 'boo'
=3D> "boo"
ruby-1.8.7-p174 > A =3D 'bar'
=3D> "bar"
ruby-1.8.7-p174 > A.freeze
=3D> "bar"
ruby-1.8.7-p174 > A =3D 'baz'
(irb):6: warning: already initialized constant A
=3D> "baz"
ruby-1.8.7-p174 > A
=3D> "baz"

Variables in Ruby are not objects, nor do they contain objects, they
refer to objects. Object#freeze prevents future changes to the state
of an object, no matter how it is referenced, and it does nothing to
prevent the (re)binding of variables.

And as for warnings vs. errors. IMHO, warnings are just that,
warnings. They warn of something which might be, and sometimes
usually will be a problem, but not always. In most cases, the
response should be to do whatever need be done to eliminate them, but
sometimes they are warnings in the sense of "fasten your seatbelt" or
"slippery when wet." Redefining a "Constant" can be useful as times.
And some time warnings, like "parenthesize argument(s) for future
version", turn out to be non-warnings when the envisioned future
version (of the Ruby parser) is abandoned.

--=20
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
 

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,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top