What is the difference between :foo and "foo" ?

C

Caleb Tennis

When I do the following:
attr_accessor :foo

For me, the colon was the most confusing part. Ignoring the colon, it
logically made sense to me that you'd want to do a:

attr_accessor foo1, foo2, foo3

Then, you realize "hey, I can't do that, because foo1, foo2, and foo3
represent local variables or methods or some object in the system".

Then I pull out my C/C++ hat and say:

attr_accessor "foo", "foo2", "foo3"

And that makes much more sense, because I have to pass the names somehow to
attr_acessor - and it's very common to using strings to do this in the C++
world.

Then you learn that every time Ruby sees a string it has to create a new
String object, and there's overhead involved. Instead, there's a special
class called Symbol which works kind of like a String, at least for these
purposes, and once you've ever created one it's always around.

Then it isn't profound until you see code like this:

1000.times do
some_object.getProperty("name")
end

and you realize that every time that gets executed a new "name" string has to
be created, and that's not so nice. But if you do a:

1000.times do
some_object.getProperty:)name)
end

Wow, that's much more fantastic.

And eventually you just "get it".
 
S

Steve Litt

I think the nomenclature of "has" isn't valid because the symbol doesn't
contain those things. Much like

a = "53"
a.to_i

=> 53

"a" doesn't contain an integer. It does, however, contain a method that
allows you to convert it to an integer representation.

I understand what you're saying Caleb. Austin -- is that what you meant?

SteveT

Steve Litt
http://www.troubleshooters.com
(e-mail address removed)
 
A

Austin Ziegler

I understand what you're saying Caleb. Austin -- is that what you meant?

Yes. I've just sent you a VERY lengthy VERY critical review of your
document that emphasizes that it is a conversion at all times. Symbols
have neither string nor integer values, but has converters.

-austin
 
C

Chad Perrin

Hopefully that's a bit clearer. It's not that different from Perl
barewords.

My understanding is that barewords are not persistent atomic elements of
code the way symbols are -- they are immediately slotted into some other
syntactic meaning by context, and if context doesn't suggest any meaning
in particular, they're assumed to be strings. In general, what this
means is that if you use a word without hints to what it is, it's a
subroutine if you have defined a subroutine by that name, and otherwise
it's just a shorthand way of using a literal string by omitting quotes.

To compare a symbol to that seems to be short-changing the potential
uses of symbols.

There is a Symbol module for Perl, which I haven't used and
unfortunately don't know much about, but my understanding is that it's
pretty limited in comparison with Lisp (or, now, Ruby) symbols, in that
it's just a wrapper for globs. It seems to me that if you want to draw
a comparison between Ruby symbols and something in Perl, globs are the
better choice, in fact, as far as the way they "act" under pressure,
though they are limited in that where a Ruby symbol is "a name" for
anything, a Perl glob is "a name" only within a filehandle context.

I haven't really thought about it before this, but I'm actually kinda
disappointed that Perl doesn't have a fully operational death star . . .
err, I mean that it doesn't have anything like symbols, except in a very
limited sense.
 
C

Chad Perrin

Note: This reply is sent directly to you, not to the list.


Excellent. Hopefully, I'm not overstepping myself by sending critiques,
but I have some suggestions:

1. The third paragraph kinda comes across as apologetic and hedging too
much. I thought about possible ways to rephrase it, but ultimately, I
think it would best be simply deleted. It doesn't seem to actually
serve any useful purpose within the text of the tutorial.

2. The "What do they resemble in other languages?" should probably have
something in it, or be removed entirely without any reference to
resembling something in another language. You might consider making a
note about it being a work in progress, with a solicitation for comments
from readers who understand symbols about what they resemble from other
languages. If you do so, I'd recommend a very strong disclaimer to the
effect that resemblance is not the same as equivalency, and one should
take care in thinking of symbols as being similar to features in other
languages. In particular, you might mention:

a. Ruby symbols bear a superficial resemblance to barewords in
Perl, but are not much the same in practice.

b. Ruby symbols bear some resemblance to globs, though globs are
limited to use in relation to filehandles. By extension (and,
unsurprisingly) Ruby symbols also bear some resemblance to the
treatment of globs by the Perl module, Symbol, though it is only an
unreasonable facsimile of Ruby symbols because it is just a wrapper
for globs meant to make them more symbol-like in appearance.

c. Ruby symbols are similar to Lisp symbols, with the exception
that in addition to the characteristics of symbols in Lisp, they are
also objects in Ruby.

3. In "What are symbols?" you might consider drawing a direct
comparison between symbols and numbers (or even integers), in particular
as regards their relationship to strings. What I mean by this is that
you might say:
:symbol is to "symbol" as 1 is to "1"

4. Another possible section to include in your tutorial might actually
be a transition between the "What do they resemble in other languages?"
and "What are symbols?" sections: "What else do they resemble in Ruby?"
This occurs to me mostly as a (perhaps better) place to put my
comparison of symbols to numbers:
:symbol is to "symbol" as 1 is to "1"

5. In the "What are symbols?" section, when you bring up the results of
:steve.to_i, you might consider mentioning that the integer to which a
symbol is translated will be different in one program than in another,
and that the reason for this is that Ruby is simply using this integer
to keep track of the symbol internally, so it's not a "random" number.
As a point of academic interest, this is because there's a hash table in
which Ruby keeps track of these things, essentially using integers as
keys by which it keeps track of symbols, though I'm not sure that's
something useful to an explanation of symbols at the level of this
tutorial. In short, I think it's a good idea to mention something about
why :symbol.to_i produces an immutably set integer so that new Rubyists
won't start making assumptions about where the number arises and try to
use it improperly as a result.

6. When you talk about capitalizing a string derived from a symbol, you
might also mention that you could, rather than using
:steve.to_s.capitalize, just use :Steve. This is more in keeping with
the point of a symbol, I think, if your capitalized version isn't meant
to have a direct connection in the program logic to :steve itself. If
it were me writing it, I might end up using an example that looks like
this, to demonstrate that from a program output perspective, both
approaches render the same result:
#!/usr/bin/env ruby
mystring = :steve.to_s.capitalize
puts mystring
puts :Steve

7. I think you forgot a colon in "A symbol is not (Just) a Name".
Either that, or I didn't understand what you were trying to demonstrate.
You also seem to have forgotten a closing parenthesis in the same
section. I might also suggest replacing the words "to hold" with "as",
when discussing the practical relationship between a symbol and a name,
since a symbol technically doesn't "hold" anything -- though I only
recommend the substitution, really, to forestall potential criticism.

8. In "What can symbols do for you?" you might consider, in addition to
mentioning that you can create a string from one using .to_s, revisiting
the fact that a symbol is essentially treated as a string when used in a
string context (such as puts :symbol).

9. In "What are the advantages and disadvantages of symbols?" you say
"The granddaddy of all advantages is also the granddaddy of
advantages:". I don't get it, unless that was a typo.

10. You might also mention another advantage of symbols, which pertains
more to metaprogramming than to the sort of programming with which most
programmers are familiar: symbols don't exist until you refer to them,
but when you do so, they spring into existence as though they had always
been there. This allows for programming logic that acts almost like
time-travel, in that you can do something with a thing that acts like
it has always existed without having to create it before you use it, and
that thing can be any arbitrary thing at all, rather than having to be
part of some intrinsic language construct. This is exactly the sort of
dynamism that makes Lisp (and, now, Ruby) so powerful.

11. I'm sure you've noticed by now, but you might consider editing the
first sentence of the "Summary" to say that symbols prompted a thread of
"more than 100 posts" rather than saying it was a "97 post thread".

Hopefully you won't take any of this amiss. Overall, I'm impressed with
the tutorial, and only recommend alterations because I'm pretty sure you
can handle it, and I would like to see your tutorial be the best it can
be. Use or ignore the foregoing advice at your leisure, of course.
 
C

Chad Perrin

Please edit your guide, as it is *not* correct when it says:

A Ruby symbol is a thing that has both a number (integer) and a string.

This is not correct in any way. If you leave it as "A Ruby Symbol is a
thing", you're fine. But when you say that it "has" a number and a
string, you are incorrect. Symbols are not composed of the integer or
string values that you can convert Symbols to; they just are. The
value of a Symbol is not its String value or its Fixnum value, but
itself.

That depends on how one understands the word "has" in this context. For
instance, as you say, a symbol does not contain or consist of parts
including an integer and a string. On the other hand, a symbol "has"
associated with it, if and when you place it within the correct context,
both a string and an integer. The string association springs into being
when one uses .to_s, and the integer association exists because of the
hash table in which symbols are stored "behind the scenes". Actually,
one might make a case for the string association existing from the word
go, but I have no idea how that could be explained succinctly.

If you would like, I can see about possibly helping you edit this or
coauthor it such that it might become a useful article to be published
at Ruby Code & Style to explain where Symbols can be used and what
they are. Ultimately, though, trying to dig into the internals of
Symbols misses the point -- it's just something that represents
itself.

I'm afraid I disagree with the implication that what strings do from
Ruby's perspective is not useful information. Simply saying that a
symbol is "something that represents itself" might explain it perfectly
to you, as it might to legions of (for instance) Python programmers used
to using names rather than variables, or whatever it is that Python
programmers do, but it clearly didn't help Steve or me very much. This
indicates that another approach is needed, at least sometimes, and that
approach might involve an understanding of *why* symbols act the way
they do in context so that symbol behavior in code can be predicted when
one is writing said code.

. . and please don't point out that symbols don't "do" anything, or
have any "behavior". You (should) know what I mean from context.
 
C

Chad Perrin

Except that by trying to suggest that Symbols "act" a particular way
is nonsense. They don't do anything, they don't have any behaviour;
they just are simple names. In 99% of all uses of Symbols, that's
absolutely all that matters.

I'm guessing you either didn't read the preceding "you know what I mean"
statement, or you don't actually know what I mean, or you're being
intentionally combative.
 
A

Austin Ziegler

I'm guessing you either didn't read the preceding "you know what I mean"
statement, or you don't actually know what I mean, or you're being
intentionally combative.

No, I *didn't* know what you meant. I still don't. Talking about the
behaviour of Symbols is nonsense. Talking about the implementation of
Symbols is (mostly) nonsense. Talking about what Symbols provide
(immediate values that are self-descriptive, like enums or consts) is
NOT nonsense. Trying to say much else about Symbols is, again, mostly
nonsense.

-austin
 
J

James Britt

Austin Ziegler wrote:
...
Do I *really* care that they're stored in Ruby on the internal symbol table?

No. Not in the last two and a half years have I cared once. Sure, it's
nice that they only end up representing a single object, but I really
have never cared how they're represented. I use them for what they
*are*, not how they're implemented. And what they are, is names.


After trying to follow all this, I've come to see Symbols as being more
akin to numbers than strings.

They're numbers with a human face.


James
--

http://www.ruby-doc.org - Ruby Help & Documentation
http://www.artima.com/rubycs/ - Ruby Code & Style: Writers wanted
http://www.rubystuff.com - The Ruby Store for Ruby Stuff
http://www.jamesbritt.com - Playing with Better Toys
http://www.30secondrule.com - Building Better Tools
 
C

Chad Perrin

No, I *didn't* know what you meant. I still don't. Talking about the
behaviour of Symbols is nonsense. Talking about the implementation of
Symbols is (mostly) nonsense. Talking about what Symbols provide
(immediate values that are self-descriptive, like enums or consts) is
NOT nonsense. Trying to say much else about Symbols is, again, mostly
nonsense.

rephrase:
"the behavior of the language as regards the use of symbols"

Does that help?

Just replace all references to the behavior of symbols with whatever is
necessary, in your world-view, to map to the concept of what happens
when you use symbols in your source code.
 
C

Chad Perrin

After trying to follow all this, I've come to see Symbols as being more
akin to numbers than strings.

They're numbers with a human face.

That's actually, from what I can see, a very good description -- though
for a complete newbie to the concept, you'd probably need to expand on
that a little bit.
 
A

Austin Ziegler

rephrase:
"the behavior of the language as regards the use of symbols"

Does that help?

Just replace all references to the behavior of symbols with whatever is
necessary, in your world-view, to map to the concept of what happens
when you use symbols in your source code.

Not really. I think James's description is pretty good, and agree that
it needs expansion. However, I don't think that the Ruby language
behaves in any particular way relating to Symbols or their use. I'm
not sure how to get it clearer than that. The Ruby language doesn't
really behave in a particular way with respect to Fixnums that impacts
most developers who *use* the language. The implementation has to deal
with them specially, and there are corner cases related to the current
implementation's use of 31 bits and the upsize and downsize between
BigDecimal, but that is, as I'm saying, an implementation issue and
not something I see as intrinsic to the language.

-austin
 
C

Chad Perrin

Not really. I think James's description is pretty good, and agree that
it needs expansion. However, I don't think that the Ruby language
behaves in any particular way relating to Symbols or their use. I'm
not sure how to get it clearer than that. The Ruby language doesn't
really behave in a particular way with respect to Fixnums that impacts
most developers who *use* the language. The implementation has to deal
with them specially, and there are corner cases related to the current
implementation's use of 31 bits and the upsize and downsize between
BigDecimal, but that is, as I'm saying, an implementation issue and
not something I see as intrinsic to the language.

Let me put it this way, then: Why don't we remove symbols from the
language?

Once you answer that, I'll take your phrasing and fit it into what I was
saying, and this ridiculous back-and-forth wherein you refuse to
communicate effectively with me can end.
 
A

Austin Ziegler

Let me put it this way, then: Why don't we remove symbols from the
language?

Strictly speaking, I do not believe that there is a *need* for them in
the language as such. They are, after all, just immediate objects.
(Which, combined with a "native" constructor syntax, is perhaps the
only reason to make them part of the interpreter.) Aside from that,
however, if they didn't exist in the language, there would likely be
some similar concept created.

Ruby-the-language probably doesn't need them as much as
ruby-the-interpreter does. But they don't actually *do* anything
special within the language, so far as I can tell. I suggest you look
at it realistically, instead of wanting to make them into something
they aren't.
Once you answer that, I'll take your phrasing and fit it into what I was
saying, and this ridiculous back-and-forth wherein you refuse to
communicate effectively with me can end.

I'm communicating. You're just not listening. Effective communication
requires a willing listener, and you seem to be uninterested in such.

-austin
 
G

Gregory Brown

I'm communicating. You're just not listening. Effective communication
requires a willing listener, and you seem to be uninterested in such.

I traded a few emails off list with Chad and he seems to basically be
only interested in telling everyone how wrong they are and how he
doesn't understand our 'inadequate' explanations.

Just killfile the annoying bastard, you've explained symbols great already.

By the way, I'm currently working on a sort of abstract document class
for Ruport that will let you use XML, YAML or pure ruby to set up a
structured document and then output it as a PDF using your library, or
in other formats.

I thought you might find that interesting. Ruport will soon depend on
PDF::Writer, as I'm going to begin putting some major PDF support to
it. If i make any progress in the way of things that can be
generalized down and put into PDF::Writer (such as charts), I'll let
you know.

Any word on the SVG status in PDF::Writer?

Cheers,
Greg
 
R

Ryan Leavengood

I traded a few emails off list with Chad and he seems to basically be
only interested in telling everyone how wrong they are and how he
doesn't understand our 'inadequate' explanations.
[further private conversation snipped]

You guys really need to watch it when you hit reply.

Also, could we for goodness sakes let this thread die?!? Please?!?

Ryan
 
J

James Britt

Ryan said:
I traded a few emails off list with Chad and he seems to basically be
only interested in telling everyone how wrong they are and how he
doesn't understand our 'inadequate' explanations.

[further private conversation snipped]

You guys really need to watch it when you hit reply.

Also, could we for goodness sakes let this thread die?!? Please?!?

Recent threads (here and elsewhere) considered, 2006 can't come soon
enough; time to start fresh.


Happy Hacking New Year!


James


--

http://www.ruby-doc.org - Ruby Help & Documentation
http://www.artima.com/rubycs/ - Ruby Code & Style: Writers wanted
http://www.rubystuff.com - The Ruby Store for Ruby Stuff
http://www.jamesbritt.com - Playing with Better Toys
http://www.30secondrule.com - Building Better Tools
 

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

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top