[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Demystifying Symbols.

Dave Howell

1/5/2006 7:26:00 AM

I was actually doing really well until the strange discussion involving
throwing constants into the mix showed up, so I'm ignoring that.

Steve's Newby Guide was excellent, if overly-complicated.

Extremely useful item (I think from Gregory, but I'm not sure):

attr_accessor "liquids", "solids"

as effective and functional as

attr_accessor :liquids, :solids



So Steve, or anybody, I've learned that I can :mysymbol.to_i and I get
an integer back. OK, I give up. What possible use do I have for this
zany parlor trick? I'll hazard a guess that, as a "normal" programmer,
not doing system-level stuff, not extending Ruby, not trying to do
something dense and clever and incomprehensible...I don't.

In fact, somebody correct me if I'm wrong, but I could actually never
use a Symbol in a single line of Ruby code and I'd still be able to get
everything done that I might reasonably want to.


Anyway, I'm quite confident I know what a Symbol is now. It's an
immutable string.

Stop!! Put your geek away! I didn't say it was a String. It's a string.
It's a Merriam-Webster "string1" 5b(2) 'series of like objects (e.g.
characters, bits, words)'. It's just a series of characters cemented
together, bless its little stable self. What Ruby calls a String is
actually some kind of pandimensional quasi-magic method-possessing
self-modifying Object, and not an ordinary string at all.

Note that the neologism
:symbol is to "symbol" as 1 is to "1"
makes not one iota of sense unless one already understands all the
complex ramifications of those quote marks. And given the extra magic
powers of double quotes vs. single quotes, well, ick.

A couple of people have tried to advance the idea that a Symbol is a
name. I think that's a terrible mistake. On the one hand, here in the
real world, people will say something like "Dave is a boy's name," but
what that sentence *really* means is "The word 'dave' is normally used
only as a name for males."

For me, :dave doesn't become a name until I decide what it's the name
*of*. :dave is just a boring old string, and when I say
attr_accessor :dave
what I'm actually saying is "Dear routine/module/function named
'attr_accessor,' please create a couple of new methods for my
Class/Module/whatever. Use that string I sent you as the name of one
which will return the value of a variable also named that, and use it
yet again to build a setter method." That'll get me a method named
"dave", a method named "dave=", and, hmm. I don't know if "dave=" will
work if I don't explicitly set @dave to a value at some point, although
I think it will. I'd have to check that to find out.

Anyway, that's not the important point. Nor is the potential
"efficiency" of Symbols vs. Strings. Good grief, have you people looked
at the millisecond differences that I've been seeing thrown around? We
beginners couldn't care less; we just want it to not make lots of
errors when it runs, or doesn't run.


What I've learned:
A Symbol is just a modest, simple string. It's optional; I could
always use a String instead. It's good for naming things that are
'inside' my program, and that won't need to be modified, input, output,
or generally fiddled with.

or

A Symbol is a non-variable variable (aka a constant) that always and
only contains its own name.


Seriously, as a newbie, that last sentence is all I need to know. And,
quite honestly, probably all I'll *ever* need to know about Symbols.






25 Answers

Chad Perrin

1/5/2006 8:13:00 AM

0

On Thu, Jan 05, 2006 at 04:25:31PM +0900, Dave Howell wrote:
>
> So Steve, or anybody, I've learned that I can :mysymbol.to_i and I get
> an integer back. OK, I give up. What possible use do I have for this
> zany parlor trick? I'll hazard a guess that, as a "normal" programmer,
> not doing system-level stuff, not extending Ruby, not trying to do
> something dense and clever and incomprehensible...I don't.

The integer value can be regarded as a side effect of the way symbols
are managed. They're stored as values in a hash table with the integers
as keys, basically. It's the atomic number-like value of the symbol,
and the "it has always been there" behavior[1] of it when first defined,
that the language feature is after -- not the integers associated with
symbols in the hash table. If you find a good use for the .to_i
behavior, though, have at it.

At least, that's my understanding.


>
> In fact, somebody correct me if I'm wrong, but I could actually never
> use a Symbol in a single line of Ruby code and I'd still be able to get
> everything done that I might reasonably want to.

Symbols are useful for metaprogramming techniques, and can be used as
well for some minor performance increases. For most of the programming
people do, however, they're far from necessary.

Note: My comment about symbols being useful for metaprogramming
techniques is based on my understanding of symbols in a Lisp context,
not a Ruby context. I'm still trying to get a handle on how analogous
the two are to each other -- a lot, clearly, but the devil's in the
details (so to speak). I'm nothing like a Ruby expert, and there's a
lot of context to learn before I can be sure about those details.


>
> Stop!! Put your geek away! I didn't say it was a String. It's a string.
> It's a Merriam-Webster "string1" 5b(2) 'series of like objects (e.g.
> characters, bits, words)'. It's just a series of characters cemented
> together, bless its little stable self. What Ruby calls a String is
> actually some kind of pandimensional quasi-magic method-possessing
> self-modifying Object, and not an ordinary string at all.
>
> Note that the neologism
> :symbol is to "symbol" as 1 is to "1"

For purposes of my analogy to that effect a few days ago, that could as
easily have been single quotes as double quotes, in case there's any
confusion on the matter. My point was the relative characteristics of a
Ruby string literal as compared with numbers and symbols.


>
> For me, :dave doesn't become a name until I decide what it's the name
> *of*. :dave is just a boring old string, and when I say
> attr_accessor :dave

This makes me want to draw analogies between Ruby symbols and spoken
language phonemes, which while interesting and in some respects accurate
would probably not be helpful to many people.


>
> A Symbol is a non-variable variable (aka a constant) that always and
> only contains its own name.
>
>
> Seriously, as a newbie, that last sentence is all I need to know. And,
> quite honestly, probably all I'll *ever* need to know about Symbols.

That depends on just how advanced your programming gets. Programs that
write themselves are nothing to sneeze at. One of these days, I'll be
at a point where I can actually take advantage of that sort of thing --
but I'm not there yet.

--
Chad Perrin [ CCD CopyWrite | http://ccd.ap... ]

This sig for rent: a Signify v1.14 production from http://www.d...


dblack

1/5/2006 12:55:00 PM

0

Its Me

1/5/2006 2:35:00 PM

0

Yet another try ...

A symbol is an object with a string name. No two symbols (with different
object_id) can have the same name string.

The literal
:x
evaluates to a symbol with name "x", creating the symbol if necessary.


"Dave Howell" <groups@grandfenwick.net> wrote in message
news:cfe9ee0530a44c3082704317b922535c@grandfenwick.net...
>I was actually doing really well until the strange discussion involving
>throwing constants into the mix showed up, so I'm ignoring that.
>
> Steve's Newby Guide was excellent, if overly-complicated.
>
> Extremely useful item (I think from Gregory, but I'm not sure):
>
> attr_accessor "liquids", "solids"
>
> as effective and functional as
>
> attr_accessor :liquids, :solids
>
>
>
> So Steve, or anybody, I've learned that I can :mysymbol.to_i and I get an
> integer back. OK, I give up. What possible use do I have for this zany
> parlor trick? I'll hazard a guess that, as a "normal" programmer, not
> doing system-level stuff, not extending Ruby, not trying to do something
> dense and clever and incomprehensible...I don't.
>
> In fact, somebody correct me if I'm wrong, but I could actually never use
> a Symbol in a single line of Ruby code and I'd still be able to get
> everything done that I might reasonably want to.
>
>
> Anyway, I'm quite confident I know what a Symbol is now. It's an immutable
> string.
>
> Stop!! Put your geek away! I didn't say it was a String. It's a string.
> It's a Merriam-Webster "string1" 5b(2) 'series of like objects (e.g.
> characters, bits, words)'. It's just a series of characters cemented
> together, bless its little stable self. What Ruby calls a String is
> actually some kind of pandimensional quasi-magic method-possessing
> self-modifying Object, and not an ordinary string at all.
>
> Note that the neologism
> :symbol is to "symbol" as 1 is to "1"
> makes not one iota of sense unless one already understands all the complex
> ramifications of those quote marks. And given the extra magic powers of
> double quotes vs. single quotes, well, ick.
>
> A couple of people have tried to advance the idea that a Symbol is a name.
> I think that's a terrible mistake. On the one hand, here in the real
> world, people will say something like "Dave is a boy's name," but what
> that sentence *really* means is "The word 'dave' is normally used only as
> a name for males."
>
> For me, :dave doesn't become a name until I decide what it's the name
> *of*. :dave is just a boring old string, and when I say
> attr_accessor :dave
> what I'm actually saying is "Dear routine/module/function named
> 'attr_accessor,' please create a couple of new methods for my
> Class/Module/whatever. Use that string I sent you as the name of one which
> will return the value of a variable also named that, and use it yet again
> to build a setter method." That'll get me a method named "dave", a method
> named "dave=", and, hmm. I don't know if "dave=" will work if I don't
> explicitly set @dave to a value at some point, although I think it will.
> I'd have to check that to find out.
>
> Anyway, that's not the important point. Nor is the potential "efficiency"
> of Symbols vs. Strings. Good grief, have you people looked at the
> millisecond differences that I've been seeing thrown around? We beginners
> couldn't care less; we just want it to not make lots of errors when it
> runs, or doesn't run.
>
>
> What I've learned:
> A Symbol is just a modest, simple string. It's optional; I could always
> use a String instead. It's good for naming things that are 'inside' my
> program, and that won't need to be modified, input, output, or generally
> fiddled with.
>
> or
>
> A Symbol is a non-variable variable (aka a constant) that always and only
> contains its own name.
>
>
> Seriously, as a newbie, that last sentence is all I need to know. And,
> quite honestly, probably all I'll *ever* need to know about Symbols.
>
>
>
>
>
>


Chad Perrin

1/5/2006 4:04:00 PM

0

On Thu, Jan 05, 2006 at 09:54:35PM +0900, dblack@wobblini.net wrote:
>
> Well, Ruby has variables and constants, and symbols aren't either of
> them :-) I guess they're constant, informally, but they're not
> constants, as defined by the language. I wouldn't call them variables
> in any sense.

I guess that, if you want to compare them to "variables" (things that
you create, define, and change at whim) and "constants" (things that you
create, define, and -- hopefully -- never change), you could think of
symbols as "forevers": from the point of view of the programmer, you
never really create or define the things, you just discover them waiting
there for you, already in the form they will always have. Heh.

There are data types, and there are data archetypes, I suppose.

That's just another broken analogy, though, and ultimately I keep
wanting to come back to some technical details related to internal hash
table behavior.

--
Chad Perrin [ CCD CopyWrite | http://ccd.ap... ]

This sig for rent: a Signify v1.14 production from http://www.d...


Steve Litt

1/5/2006 8:55:00 PM

0

On Thursday 05 January 2006 02:25 am, Dave Howell wrote:
> I was actually doing really well until the strange discussion involving
> throwing constants into the mix showed up, so I'm ignoring that.
>
> Steve's Newby Guide was excellent, if overly-complicated.
>
> Extremely useful item (I think from Gregory, but I'm not sure):
>
> attr_accessor "liquids", "solids"
>
> as effective and functional as
>
> attr_accessor :liquids, :solids

Yeah, that was quite a breakthrough for me too. I coded it to prove it to
myself, and from that moment on I found that I knew when to use Symbols, and
what I could accomplish by using them.

>
> So Steve, or anybody, I've learned that I can :mysymbol.to_i and I get
> an integer back. OK, I give up. What possible use do I have for this
> zany parlor trick? I'll hazard a guess that, as a "normal" programmer,
> not doing system-level stuff, not extending Ruby, not trying to do
> something dense and clever and incomprehensible...I don't.

Hi Dave,

I personally know of no use, in *my* application as opposed to Ruby internals,
for the integer representation of a symbol.

>
> In fact, somebody correct me if I'm wrong, but I could actually never
> use a Symbol in a single line of Ruby code and I'd still be able to get
> everything done that I might reasonably want to.

Except for giving up symbols' memory advantages and (probably slight)
performance advantages, that's my understanding also.
>
>
> Anyway, I'm quite confident I know what a Symbol is now. It's an
> immutable string.
>
> Stop!! Put your geek away! I didn't say it was a String. It's a string.
> It's a Merriam-Webster "string1" 5b(2) 'series of like objects (e.g.
> characters, bits, words)'. It's just a series of characters cemented
> together, bless its little stable self. What Ruby calls a String is
> actually some kind of pandimensional quasi-magic method-possessing
> self-modifying Object, and not an ordinary string at all.

I don't think it would hurt to think of it as an immutable string (not a
String), in your own personal life. However, that probably would not go over
well on a mailing list :-)


>
> Note that the neologism
>
> :symbol is to "symbol" as 1 is to "1"
>
> makes not one iota of sense unless one already understands all the
> complex ramifications of those quote marks. And given the extra magic
> powers of double quotes vs. single quotes, well, ick.

I never understood that analogy.

[clip]

> A Symbol is a non-variable variable (aka a constant) that always and
> only contains its own name.

That's an interesting and concise statement. I'll have to think about that.

SteveT

Steve Litt
http://www.troublesh...
slitt@troubleshooters.com


Steve Litt

1/5/2006 8:56:00 PM

0

On Thursday 05 January 2006 03:13 am, Chad Perrin wrote:
> On Thu, Jan 05, 2006 at 04:25:31PM +0900, Dave Howell wrote:
> > So Steve, or anybody, I've learned that I can :mysymbol.to_i and I get
> > an integer back. OK, I give up. What possible use do I have for this
> > zany parlor trick? I'll hazard a guess that, as a "normal" programmer,
> > not doing system-level stuff, not extending Ruby, not trying to do
> > something dense and clever and incomprehensible...I don't.
>
> The integer value can be regarded as a side effect of the way symbols
> are managed. They're stored as values in a hash table with the integers
> as keys, basically. It's the atomic number-like value of the symbol,
> and the "it has always been there" behavior[1] of it when first defined,
> that the language feature is after -- not the integers associated with
> symbols in the hash table. If you find a good use for the .to_i
> behavior, though, have at it.
>
> At least, that's my understanding.

Internals question: Curious -- why don't they hash with the object id instead?

SteveT

Steve Litt
http://www.troublesh...
slitt@troubleshooters.com


Steve Litt

1/5/2006 9:10:00 PM

0

On Thursday 05 January 2006 03:55 pm, Steve Litt wrote:
> On Thursday 05 January 2006 03:13 am, Chad Perrin wrote:
> > On Thu, Jan 05, 2006 at 04:25:31PM +0900, Dave Howell wrote:
> > > So Steve, or anybody, I've learned that I can :mysymbol.to_i and I get
> > > an integer back. OK, I give up. What possible use do I have for this
> > > zany parlor trick? I'll hazard a guess that, as a "normal" programmer,
> > > not doing system-level stuff, not extending Ruby, not trying to do
> > > something dense and clever and incomprehensible...I don't.
> >
> > The integer value can be regarded as a side effect of the way symbols
> > are managed. They're stored as values in a hash table with the integers
> > as keys, basically. It's the atomic number-like value of the symbol,
> > and the "it has always been there" behavior[1] of it when first defined,
> > that the language feature is after -- not the integers associated with
> > symbols in the hash table. If you find a good use for the .to_i
> > behavior, though, have at it.
> >
> > At least, that's my understanding.
>
> Internals question: Curious -- why don't they hash with the object id
> instead?

Oh never mind -- a more careful reading of Evan's internals explanation makes
it clear.

SteveT

Steve Litt
http://www.troublesh...
slitt@troubleshooters.com


Jim Weirich

1/5/2006 9:47:00 PM

0

Dave Howell wrote:
[...]
> A Symbol is a non-variable variable (aka a constant) that always and
> only contains its own name.

Heh. That reminds me of the quote:

The problem with programming is that
variables don't and constants aren't."

Perhaps the word you are looking for is "literal". I.e. the sequence of
characters (':', 'x', 'y', 'z') is the literal representation of the
symbol :xyz. (Just as you would say the sequence of characters ('1',
'2', '3') is the literal presentation of the number 123).

--
-- Jim Weirich

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


Lyndon Samson

1/5/2006 11:45:00 PM

0

>
> I personally know of no use, in *my* application as opposed to Ruby
> internals,
> for the integer representation of a symbol.



How about logging method names in a human readable manner using id2name ?

method_missing(id, *args)

id: method symbol
*args: method arguments

Dave Howell

1/6/2006 2:41:00 PM

0


On Jan 5, 2006, at 12:54, Steve Litt wrote:

>
> I personally know of no use, in *my* application as opposed to Ruby
> internals,
> for the integer representation of a symbol.

Exactly. So if I'm going to try explaining Symbols to somebody who's
still trying to learn Ruby, I would completely omit any mention of the
fact you could .to_i a Symbol.

>> In fact, somebody correct me if I'm wrong, but I could actually never
>> use a Symbol in a single line of Ruby code and I'd still be able to
>> get
>> everything done that I might reasonably want to.
>
> Except for giving up symbols' memory advantages and (probably slight)
> performance advantages, that's my understanding also.

I'm a newby; I'm not to the point that I care about memory advantages.
I have a gigabyte to burn. Performance advantages appear to be nearly
indetectable. Long before Symbols make a difference, I probably ought
to learn many other far more relevant programming tricks to get more
speed.

> I don't think it would hurt to think of it as an immutable string (not
> a
> String), in your own personal life. However, that probably would not
> go over
> well on a mailing list :-)

Yes, well, the fact that this mailing list was full of the most
horrific dis-explanations of Symbols is one of the things that prompted
me to *not* keep my thoughts to myself.


> [clip]
>
>> A Symbol is a non-variable variable (aka a constant) that always and
>> only contains its own name.
>
> That's an interesting and concise statement. I'll have to think about
> that.

Somebody else suggested thinking of them as "forevers," which is pretty
cute.

A variable is a box. You can put a sign on the box, like "current_user"
or "amountPaid," and then put something in the box, put something else
in, and on and on. (Yes, in Ruby one actually puts references in the
box, not things. That is also a layer of complication inappropriate in
an explanation to a newbie, although it can't be put off for long.)

A constant is a wooden crate. You take something, build the crate
around it, then stick the sign on the outside. Put a watermelon in a
crate. Label it "oranges" if you like, but you can't put something else
in once you've built it.

A symbol is a crate with no label, but a clear cover. What you see is
what you get; the label is also the contents.


To restate the one-liner above:

A Symbol is an unalterable (*) that always and only contains its own
name.

Damn. I can't find any word besides "variable" to go in that spot.
"Container" is the functional description, and in programming, the
first kind of container you meet (in every language I know) is called a
variable, unless you learned Assembly first, and then it'd be
"register." I can't support "literal," since as a programming term, I
think it's quite obscure, which means I have to default to the general
English meaning, and it's not remotely literal in that sense.
Otherwise, I'd be able to sit down on a :chair. :)




P. S. With duck-typing as a Virtue in Rubyville, when am I ever going
to be presented with a method that won't accept a String as well as a
Symbol? Would there ever be a reason besides "bad programming" to be
that restrictive?


P. P. S. The "contains itself" aspect of Symbols reminds me of Rexx. In
Rexx, you can just use a variable without declaring it first, like
Ruby. Unlike Ruby, a "new" Rexx variable doesn't contain "nil". It's
initialized to its own name as a string. This would occasionally result
in the most *peculiar* error messages.