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?

···

--
Posted via http://www.ruby-forum.com/.

Hi --

···

On Tue, 22 Dec 2009, Sonja Elen Kisa 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

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

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 Dec 21, 2009, at 4:57 PM, Sonja Elen Kisa 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?
--
Posted via http://www.ruby-forum.com/\.

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

···

On Tue, Dec 22, 2009 at 11:57 AM, Sonja Elen Kisa <sonja@kisa.ca> 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?
--
Posted via http://www.ruby-forum.com/\.

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?

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

···

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

--
Bertram Scharpf
Stuttgart, Deutschland/Germany
*
Discover String#notempty? at <http://raa.ruby-lang.org/project/step&gt;\.

Should I only use symbols for hash keys and method names?

···

--
Posted via http://www.ruby-forum.com/.

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 Tue, Dec 22, 2009 at 9:07 AM, Gennady Bystritsky <Gennady.Bystritsky@quest.com> wrote:

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.

···

On Dec 21, 2009, at 8:07 PM, Gennady Bystritsky wrote:

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 Dec 21, 2009, at 4:57 PM, Sonja Elen Kisa 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?
--
Posted via http://www.ruby-forum.com/\.

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.

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

ruby-1.8.7-p174 > x = 'foo'
=> "foo"
ruby-1.8.7-p174 > x.freeze
=> "foo"
ruby-1.8.7-p174 > x = 'boo'
=> "boo"
ruby-1.8.7-p174 > A = 'bar'
=> "bar"
ruby-1.8.7-p174 > A.freeze
=> "bar"
ruby-1.8.7-p174 > A = 'baz'
(irb):6: warning: already initialized constant A
=> "baz"
ruby-1.8.7-p174 > A
=> "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.

···

On Tue, Dec 22, 2009 at 6:02 AM, Bertram Scharpf <lists@bertram-scharpf.de> wrote:

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)

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

Hi,

···

Am Dienstag, 22. Dez 2009, 23:00:48 +0900 schrieb Sonja Elen Kisa:

Should I only use symbols for hash keys and method names?

Today, I almost used floats as hash keys. I was really frightened
when I noticed what I was doing.

Bertram

--
Bertram Scharpf
Stuttgart, Deutschland/Germany
*
Discover String#notempty? at <http://raa.ruby-lang.org/project/step&gt;\.

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.

···

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

On Tue, Dec 22, 2009 at 9:07 AM, Gennady Bystritsky > <Gennady.Bystritsky@quest.com> 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?

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.

···

-----Original Message-----
From: Young H. [mailto:armywide@gmail.com]
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?

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

···

On Mon, Dec 21, 2009 at 8:15 PM, Young H. <armywide@gmail.com> wrote:

On Tue, Dec 22, 2009 at 9:07 AM, Gennady Bystritsky > <Gennady.Bystritsky@quest.com> 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?

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

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
marnen@marnen.org
--
Posted via http://www.ruby-forum.com/\.

Hi,

···

Am Dienstag, 22. Dez 2009, 21:59:30 +0900 schrieb Rick DeNatale:

On Tue, Dec 22, 2009 at 6:02 AM, Bertram Scharpf > <lists@bertram-scharpf.de> wrote:
>
> Constants are variables:
>
> X = "hello"
> X.freeze
> X.replace "hi again" # forbidden (TypeError)
>
> But you can also freeze other variables:
>
> x = "foo"
> x.freeze
> x.replace "bar" # forbidden (TypeError)

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

Yes, of course. I meant 'objects in regular, non-constant
variables'. Sorry for the sloppy phrase.

I just detected another thing: It is abolutely pointless to freeze
a symbol _object_.

Bertram

--
Bertram Scharpf
Stuttgart, Deutschland/Germany
*
Discover String#notempty? at <http://raa.ruby-lang.org/project/step&gt;\.

The great thing about warnings, in my opinion, is that they sometimes find bugs for free. That only works though, if you notice the warning. Thus, you need to keep the noise level down so you can see the warning when it is given. To me, that means resolving all warnings as they come up.

It's interesting to note that what I just described is pretty much standard operating procedure for Perl programmers now. You'll be hard pressed to find modern Perl literature that doesn't show warnings turned on and avoided in their code. The same is dramatically less common for us. This is the one place I feel their culture is superior to ours.

The most important aspect to me is libraries though. If you write a library that throws warnings, I think we should be able to take your keyboard away. It's the whole second-hand smoke thing. You didn't just make a bad choice for you but for me too.

It's also worth noting that it is possible to change a constant without a warning. In fact, it's possible to do all of Ruby's dynamic magic without warnings, so you certainly can avoid them if you desire.

James Edward Gray II

···

On Dec 22, 2009, at 6:59 AM, Rick DeNatale wrote:

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.

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 Gray II

···

On Dec 21, 2009, at 7:38 PM, Gennady Bystritsky 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.

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

···

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

-----Original Message-----
From: Young H. [mailto:armywide@gmail.com]
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?

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

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

···

On Tue, Dec 22, 2009 at 1:58 PM, Marnen Laibow-Koser <marnen@marnen.org> 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).