[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: does ":" have an anolog in another language?

Charles Hoffman

7/28/2006 7:53:00 PM

Sorry if anyone's beat me to this, I find it hard to keep up with such a busy list...

: is used to denote a symbol. The idea of a symbol has a rather broad interpretation, but I think that technically any identifier you make in a program -- a variable name or function name, for instance -- is a symbol.

Lisp languages have symbols in a similar sense that Ruby does, and they are usually used in much the same capacity as variable names -- they get evaluated into any binding they currently have to a function or piece of data. However, if quoted with a single quote (like 'this) they are left unevaulated and treated as the symbols themselves.

Lisp symbols don't have to start with :, but certain uses of them where they have semantic significance use that convention, which might be where Ruby borrows the syntax from. For example, a common idiom in Common Lisp is the associative list, where elements are alternated with symbols that must start with : and elements can be referenced by the :-symbol right before them. So you might have a list like (:a 1 :b 2 :c 3) called mylist, and (getf mylist :b) will return the 2. Similarly, :-symbols are used a lot in argument lists for many Common Lisp functions, to denote values meant to be passed to named parameters (aka keyword parameters).

I think there's also something like it in Smalltalk but I'm not sure.

So basically, a symbol is this little constant singleton identifier thing, that you can use to mean whatever ;)

--ch--

-------------- Original message ----------------------
From: "Matt Todd" <chiology@gmail.com>
> A simple way to describe it may be to look at it as a singleton object
> in that, every unique symbol is a different object, but all of the
> same symbols are the same object.
>
> For instance...
>
> x = Foo.new('bar')
> y = Foo.new('bar')
> z = Foo.new('baz')
>
> Both x and y will be the same object, but different references. But z
> will be different because its value is different. The only way I can
> think of reproducing this in any other language is to create this
> weird version of a Singleton class. Should we call it a
> Unique-Singleton pattern?
>
> M.T.
>



10 Answers

dblack

7/28/2006 8:13:00 PM

0

Ike

7/29/2006 1:11:00 AM

0


<nothinghappens@mchsi.com> wrote in message
news:072820061953.19665.44CA6B2700073D1500004CD1219791280203010CD2079C080C03BF9C020A9F9F0E08090207089B0102@mchsi.com...
> Sorry if anyone's beat me to this, I find it hard to keep up with such a
> busy list...
>
> : is used to denote a symbol. The idea of a symbol has a rather broad
> interpretation, but I think that technically any identifier you make in a
> program -- a variable name or function name, for instance -- is a symbol.
>
> Lisp languages have symbols in a similar sense that Ruby does, and they
> are usually used in much the same capacity as variable names -- they get
> evaluated into any binding they currently have to a function or piece of
> data. However, if quoted with a single quote (like 'this) they are left
> unevaulated and treated as the symbols themselves.
>
> Lisp symbols don't have to start with :, but certain uses of them where
> they have semantic significance use that convention, which might be where
> Ruby borrows the syntax from. For example, a common idiom in Common Lisp
> is the associative list, where elements are alternated with symbols that
> must start with : and elements can be referenced by the :-symbol right
> before them. So you might have a list like (:a 1 :b 2 :c 3) called
> mylist, and (getf mylist :b) will return the 2. Similarly, :-symbols are
> used a lot in argument lists for many Common Lisp functions, to denote
> values meant to be passed to named parameters (aka keyword parameters).
>
> I think there's also something like it in Smalltalk but I'm not sure.
>
> So basically, a symbol is this little constant singleton identifier thing,
> that you can use to mean whatever ;)
>
> --ch--
>

Doesn;t that therefore make it the same thing as a reference in c++ ? -Ike


Keith Gaughan

7/29/2006 2:00:00 AM

0

On Sat, Jul 29, 2006 at 10:15:09AM +0900, Ike wrote:

> Doesn;t that therefore make it the same thing as a reference in c++ ?

No, because unlike a reference, a symbol doesn't actually refer to
anything. A symbol has no meaning other than that which you give it in
your head. A symbol is _just_ like a string except that (a) there's only
one instance of each, i.e., :foo is always the same symbol whereever it
is in the program whereas "foo" is different; and (b) they're immutable:
you can't ever change a symbol.

K.

--
Keith Gaughan - kmgaughan@eircom.net - http://tal...
So far as we are human, what we do must be either evil or good: so far
as we do evil or good, we are human: and it is better, in a paradoxical
way, to do evil than to do nothing: at least we exist.
-- T. S. Eliot, essay on Baudelaire

Matt Todd

7/29/2006 4:01:00 AM

0

Think of a uniquely identifiable, primary key in a database. You would
only refer to that row in that table with that one unique identifier,
right? Imagine that that id is 20. 20 is a pretty arbitrary value, and
can mean 20 people, 20 dollars, 20 minutes past 8, or even 20 the
number itself. That said, the meaning is determined by the context and
intention. Computers don't care too much about intention, and context
only when it has to, so 20 is what it is: a symbol referring to
something. In some cases, a string, in others, a number, and in fewer
circumstances, a symbol (in Ruby).

So, looking at 20 as a symbol (which would be created like... :"20"),
we'd be able to say that this symbol can represent anything, but it is
still the same thing. There is no need to create new instances of it
for the different contexts, really, because it still means nothing
apart from the context and the intention.

Back at the table, we can say that we want to mess with the row
identified by 20. This arbitrary value does not necessarily have an
immediate meaning on the rest of the data (in this example), but it
has meaning in that it identifies it. So, 20 can identify it in this
instance, and it won't in others.

But, what I really want to show is that the symbol, :"20", in ruby, is
always considered the symbol 20, and is not unique in any
circumstances. You mention the symbol :"20", and you will always use
the same symbol (as can be seen by its object id). It is all the same,
but, again, the context is what distinguishes its inherent value.

Of course, this is clear in my head: I hope I described it plainly
enough for you, though I feel somehow that I might not have.

M.T.

Charles Hoffman

7/29/2006 1:59:00 PM

0

> On Sat, Jul 29, 2006 at 10:15:09AM +0900, Ike wrote:
>
> > Doesn;t that therefore make it the same thing as a reference in c++ ?

In Lisp,. the way I was explaining it, yes -- in the sense that they are
what Lisp uses for variables. In Ruby, they are more like the
occurrences of symbols in Lisp in which they typically start with a : --
they mean whatever you decide they mean. You might use them to index a
collection (e.g. a Hash) or you can assign them whatever semantic
meaning suits your intentions.

--ch--


Aleks Kissinger

7/30/2006 8:53:00 AM

0

As far as an analogue in other languages, I tend to think of symbols
sortof like old-style C defines.

#define HAS_EGGS 1
#define CAN_FRY 2
add_property(HAS_EGGS);
add_property(CAN_FRY);

...or y'know something like that. Its a constant that has some
meaning, but all you really care about is it's unique.

add_property(:has_eggs)
add_property(:can_fry)

But all the #defines and stuff are automatic. You could use strings,
sure, but just like in C, I sortof get this sickly feeling when I use
(slow) strings when I don't have to. This explanation skips over all
the crazy OO stuff you can do with symbols, but I'd say thats the
closest thing you can get to their equivalent in another language.

On 7/29/06, Charles Hoffman <nothinghappens@mchsi.com> wrote:
> > On Sat, Jul 29, 2006 at 10:15:09AM +0900, Ike wrote:
> >
> > > Doesn;t that therefore make it the same thing as a reference in c++ ?
>
> In Lisp,. the way I was explaining it, yes -- in the sense that they are
> what Lisp uses for variables. In Ruby, they are more like the
> occurrences of symbols in Lisp in which they typically start with a : --
> they mean whatever you decide they mean. You might use them to index a
> collection (e.g. a Hash) or you can assign them whatever semantic
> meaning suits your intentions.
>
> --ch--
>
>
>

Charles Hoffman

7/30/2006 4:56:00 PM

0

On Sun, 2006-07-30 at 17:52 +0900, Aleks Kissinger wrote:
> As far as an analogue in other languages, I tend to think of symbols
> sortof like old-style C defines.
>
> #define HAS_EGGS 1
> #define CAN_FRY 2
> add_property(HAS_EGGS);
> add_property(CAN_FRY);

I hadn't thought of that, but much of the typical usage is somewhat
similar. But Ruby's symbols can also be used to refer to an existing
attribute or method by name... you see a lot of that sort of thing in
_why's stuff.

--ch--


dblack

7/30/2006 5:15:00 PM

0

Rick DeNatale

8/10/2006 4:49:00 PM

0

On 8/9/06, Jürgen Strobel <strobel@secure.at> wrote:

> As Ruby started out as a simple Lisp dialect, that's certainly where
> :symbols come from.

I'm not sure what Matz would say about this. Although Lisp was
certainly one influence, it's pretty clear that Smalltalk was at
least, if not more so. One hint is the naming of the iterators in
enumerable, and the use of blocks. Not to mention the ruby object
model, which can be seen as another evolutionary step in the chain
started through Smalltalk-72, Smalltalk-76, and Smalltalk-80, with a
few capabilities of Self mixed in. Now, Lisp of course had a great
deal of influence on Alan Kay and Dan Ingalls in conceiving and
developing Smalltalk as well, but I don't think that it's accurate to
say that "Ruby started out as a simple Lisp dialect." As I understand
it started out as a scripting language which was completely
object-oriented in the way Kay intended when he coined the term. What
I sense as the family-tree of ruby, through the glasses of an old
Smalltalker, looks something like:

+--------------------+
/ Perl ------- Lisp --> Smaltalk --\--> Ruby
\- Self -/

But that's an archaeological analysis, with a certain bias on my part.

--
Rick DeNatale

IPMS/USA Region 12 Coordinator
http://ipmsr12.denh...

Visit the Project Mercury Wiki Site
http://www.mercuryspace...

Rick DeNatale

8/10/2006 4:56:00 PM

0

And by the way the equivalent to

:foo

in Smalltalk

is #foo

of course ruby would see this as a comment hence :foo instead

--
Rick DeNatale