Does ":" have an anolog in another language?

does the colon operator have an anolog in say, Java or C++ ? It seems to
be
a reference pointer. Am i mistaken in assuming this? Thanks, Ike

On Sat, Jul 29, 2006 at 03:40:11AM +0900, Ike wrote:

does the colon operator have an anolog in say, Java or C++ ? It seems to be
a reference pointer. Am i mistaken in assuming this? Thanks, Ike

I tend to guess you’re referring to uses of the : character such as the
following . . .

foo = { :bar => “baz” }

If that’s what you mean, it’s not an operator. Rather, it’s a sigil,
used to denote a symbol. Symbols are subtle things, and last time I
personally saw them discussed and defined on ruby-talk it turned into a
flamewar. I recommend googling for “ruby symbol” (without the quotes)
and reading about them thusly.

On Sat, Jul 29, 2006 at 04:18:28AM +0900, Matt T. wrote:

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?

I’m perfectly happy with “object-oriented symbol”, but that might be
because Ruby is (100%-ish) object-oriented, and I knew about symbols in
the Lisp idiom before encountering Ruby. Ruby symbols really do just
appear to be the same thing as Lisp symbols, except you can send them
messages in the Ruby idiom.

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.

Ike wrote:

does the colon operator have an anolog in say, Java or C++ ? It seems to be
a reference pointer. Am i mistaken in assuming this? Thanks, Ike

Tokens that start with colon are called “symbols”.

Check out the Symbol class at ruby-doc: RDoc Documentation

@Chad:

Yep, that’s about it, as far as I can tell.

Hi –

On Sat, 29 Jul 2006, Chad P. wrote:

messages in the Ruby idiom.
I’d just call them symbols, or Symbol objects (just as with strings,
arrays, etc.).

David

On Sat, 29 Jul 2006 [email protected] wrote:

I’d just call them symbols, or Symbol objects (just as with strings, arrays,
etc.).

i like ‘symbol literal’ as in

array literal => [42]
hash literal => {42=>42}
symbol literal => :foo

2 cts.

-a

On Sat, Jul 29, 2006 at 05:07:16AM +0900, [email protected] wrote:

On Sat, 29 Jul 2006, Chad P. wrote:

I’m perfectly happy with “object-oriented symbol”, but that might be
because Ruby is (100%-ish) object-oriented, and I knew about symbols in
the Lisp idiom before encountering Ruby. Ruby symbols really do just
appear to be the same thing as Lisp symbols, except you can send them
messages in the Ruby idiom.

I’d just call them symbols, or Symbol objects (just as with strings,
arrays, etc.).

Even better – more succinct, makes the same point. Thanks.

On Sat, Jul 29, 2006 at 05:11:35AM +0900, [email protected] wrote:

symbol literal => :foo
. . . except that doesn’t point out the object-orientedness of it. The
reason I brought up attaching something related to object oriented
programming to the word “symbol” was as a means of contrasting with an
older and, in many cases, better-known use of “symbols” in programming
(a use that is, in fact, almost identical except for that pesky OO-ness
of Ruby).

Hi –

On Sat, 29 Jul 2006, Matt T. wrote:

Both x and y will be the same object, but different references.
In the general case (i.e., unless you override Foo.new), x and y will
be different objects:

class C
def initialize(x)
end
end

p C.new(‘a’).object_id
p C.new(‘a’).object_id

You’ll get different numbers.

David

In my example, using a symbol would be the same as creating a symbol
object (whose initialize() method was overwritten to pull a
Unique-Singleton deal):

{ :foo => “bar” }

is the same as

{ Symbol.new(‘foo’) => “bar” }

(That is, if Symbol had #new defined).

It’s like saying…

“Hello”

does the same thing as

String.new(‘hello’)

except that it doesn’t create a new object for duplicate values, it
simply returns the reference to the previous Symbol object created.

M.T.

Hi –

On Sat, 29 Jul 2006, Matt T. wrote:

(That is, if Symbol had #new defined).
simply returns the reference to the previous Symbol object created.
Sorry, I’m not following. What purpose would the hypothetical
Symbol.new serve?

David

Really as an example of its objectness. It’s a way to look at the
literal (:symbol_name) as the same as an instanciation, but with
conditions (of uniqueness).

I’m just sharing how I look at it.

M.T.

On Sat, Jul 29, 2006 at 03:40:11AM +0900, Ike wrote:

does the colon operator have an anolog in say, Java or C++ ? It seems to be
a reference pointer. Am i mistaken in assuming this? Thanks, Ike

As mentioned elsewhere, Smalltalk and Lisp have operators which allow
you to do this, but seeing as nobody’s mentioned Prolog, I thought I
would.

Atoms (which is the Prolog equivalent) aren’t normally introduced by or
wrapped by anything. They’re determined by their form, i.e., an
alphanumeric string that starts with a lowercase letter. thisIsAnAtom
would be an example of an atom. Variables in Prolog start with an
uppercase letter, so ThisIsAVariable would be a variable.

If the atom contains non-alphanumeric characters or starts with a number
or uppercase letter, it needs to be wrapped in single quotes, so ‘123’
would be an atom, as would ‘CHUNKY BACON!’ and ‘zing! zoop!’. Strings
are
wrapped in double quotes.

What the colon operator introduces are a special form of string. Unlike
regular strings, they’re immutable and there’s only ever one copy of it
in memory. All objects in Ruby are passed around as references, so yeah,
you’re in error in that regard, though it’s understandable.

K.

Hi –

On Sat, 29 Jul 2006, Matt T. wrote:

Really as an example of its objectness. It’s a way to look at the
literal (:symbol_name) as the same as an instanciation, but with
conditions (of uniqueness).

You might as well save the round trip, though :slight_smile: It’s in the same
category as integers: unique and immediate, as well as immutable.

David

Hi –

On Sat, 29 Jul 2006, Matt T. wrote:

Indeed. I understand that, just trying to illustrate it to those
familiar with objects moreso than symbols, atoms, or whatever they are
called. :slight_smile:

I see what you mean. Symbols do seem to present problems – they’re
sort of string-like, sort of identifier-like, sort of integer-like…
yet not exactly like any of those.

David

Indeed. I understand that, just trying to illustrate it to those
familiar with objects moreso than symbols, atoms, or whatever they are
called. :slight_smile:

@Keith: thanks for that bit on Prolog.

So far, I think we’ve gotten a pretty good understanding of symbols,
regardless of how we choose to represent it in our mind.

:slight_smile:

M.T.

[email protected] wrote:

David

The most salient symbol example for me has always been:

irb --simple-prompt

“foo”.object_id
=> 23208408

“foo”.object_id
=> 23205828

:foo.object_id
=> 5896462

:foo.object_id
=> 5896462

Tom

“Ike” [email protected] wrote in message
news:[email protected]

does the colon operator have an anolog in say, Java or C++ ? It seems to
be a reference pointer. Am i mistaken in assuming this? Thanks, Ike

No wonder this language is and has been taking off! Not only is the
language
impressive, but the enthusiasm behind it even more so. I’m really glad I
chose to start immersing myself in this. Thank you to everyone for all
of
your help in explaining this to me. -Ike