Does ":" have an anolog in another language?

Hi –

On Sat, 29 Jul 2006, Ike wrote:

Ok…so why not just use a String then, and test for equality of the values
of the String? I mean, if this is something which has no anolog, say, in
Java, or c++ (although these do seem like refrence pointers to me, ie.
pointers whose l-lvalue we cannot acces) then why use them? -Ike

Well… if they’re not useful, then I don’t think it matters whether
or not they have an analog in Java or C++ – and, come to think of it,
the same thing is true if they are useful :slight_smile:

Ruby exposes symbols for our use, as I understand it, in large part
because they are processed faster internally than strings, and less
consumptive of memory than strings. If you use a symbol twice:

hash[:a] = 1
puts hash[:a]

you’re using the same symbol object twice. If you do the same with a
string:

hash[“a”] = 1
puts hash[“a”]

you’re creating two strings objects.

People also like symbol literals in some contexts, especially as hash
keys, because of how they look. (I have no strong feelings about that
one way or the other.)

David

“Tim H.” [email protected] wrote in message
news:[email protected]

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

Ok…so why not just use a String then, and test for equality of the
values
of the String? I mean, if this is something which has no anolog, say, in
Java, or c++ (although these do seem like refrence pointers to me, ie.
pointers whose l-lvalue we cannot acces) then why use them? -Ike

[email protected] writes:

On Sat, 29 Jul 2006, Ike wrote:

Ok…so why not just use a String then, and test for equality of the values
of the String? I mean, if this is something which has no anolog, say, in
Java, or c++ (although these do seem like refrence pointers to me, ie.
pointers whose l-lvalue we cannot acces) then why use them? -Ike

Ruby exposes symbols for our use, as I understand it, in large part
because they are processed faster internally than strings, and less
consumptive of memory than strings.

Note that for this particular use case - faster internal processing on
comparisons, less duplicated memory, etc. - Java does have a weak
analog of Symbol in the method String.intern(). That method returns a
String (possibly this) that is equal to this, but has the property
that for any two strings s and t, s.equals(t) implies s.intern() ==
t.intern(). Note that the java compiler interns all constant string
expressions, so that this junit test would pass:

public static void testCompilerStrings()
{
String a = “A”;
String a2 = “A”;

assertSame("Constant Strings comparison", a, a2);

String b  = a + a;
String b2 = a + a;

assertEquals("Dynamic Strings object equality", b, b2);
assertNotSame("Dynamic Strings sameness", b, b2);

}

Indeed, in ruby the method String#intern does the same thing as
String#to_sym. (One is just an alias for the other)

People also like symbol literals in some contexts, especially as hash
keys, because of how they look. (I have no strong feelings about that
one way or the other.)

I like to avoid hitting the shift key if I can - I don’t touch-type
properly and so often my fingers are accustomed to hitting the wrong
shift key for certain key combinations. (That is, I’ll often try to
hit shift with the same hand for things like ") Writing a hash
literal already exercises this bad typing habit enough with all the >
signs in =>. It becomes easier on my hands to use symbols as keys in
hash literals. (I suppose I could start to use single quoted strings,
but I just find typing a : easier - my pinky is already there, and so
that symbol I type correctly, with my left hand hitting the shift key)

Hrm, I suppose there’s another aspect to the long “Write it in C”
thread - for some of us, writing in C is actually physically painful.
In all fairness, so is writing in Ruby, but significantly less so,
since there’s so much less code.

Symbols can best be described as identities. A symbol is all about who
it is, not what it is. Fire up irb and see the difference:

Many persons use advanced language to explain Symbols to make it more
difficult.
For me, it is a text string like any other text string. But you can not
manipulate it like split, sub!, etc… There are only 4 methods
(id2name inspect to_i to_s) for it.

Use it for hash key instead of normal string. (we do not need all
manipulating functions for hash key)

Is it simple enough?

On 9 Aug 2006, at 15:15, ngoc wrote:

manipulating functions for hash key)

Is it simple enough?

What works for me is to think of a symbol as a uniqued constant (or
immutable) string. This may depend on my language background,
however (Objective C, largely).

Paul

On Thu, 10 Aug 2006, ngoc wrote:

manipulating functions for hash key)

Is it simple enough?

There’s (at least) one more key fact (which was hidden inside of the
‘use
it for hash key instead of normal string’) – there is only one instance
of a given symbol allocated in memory at a time, which can be a great
boon
to writing efficient code.

Nate

On 7/28/06, Ike [email protected] 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

My take on explaining Symbols (from the “From other languages” page on
new.ruby-lang.org):

Many Ruby newbies struggle with understanding what Symbols are, and
what they can be used for.

Symbols can best be described as identities. A symbol is all about who
it is, not what it is. Fire up irb and see the difference:

irb(main):001:0> :george.object_id == :george.object_id
=> true
irb(main):002:0> “george”.object_id == “george”.object_id
=> false
irb(main):003:0>

The object_id methods returns the identity of an Object. If two
objects have the same object_id, they are the same (point to the same
Object in memory).

As you can see, once you have used a Symbol once, any Symbol with the
same characters references the same Object in memory. For any given
two Symbols that represent the same characters, the object_ids match.

Now take a look at the String (“george”). The object_ids don’t match.
That means they’re referencing two different objects in memory.
Whenever you use a new String, Ruby allocates memory for it.

If you’re in doubt whether to use a Symbol or a String, consider
what’s more important: the identity of an object (ie. a Hash key), or
the contents (in the example above, “george”).

So a Symbol is like an identity, or an interned string, which is used
by Ruby and is available for your use internally in your program.
Think of using an enum: you’re really just giving names for your own
good, the program doesn’t care whether you key something with
cars.ferrari or the number 3, as long as it’s used consistently.

Ike,

in Java:

String a = “Hello”;
String b = new String(“Hello”);
boolean notSame = a == b; // false, the object references are not the
same

String a = “Hello”.intern();
String b = (new String(“Hello”)).intern();
boolean same = a == b; // true, the object references are the same

Intern and Symbol do something very similar. They convert the string
to an Object that has the same object reference.

However, in Ruby, symbols are much faster and more efficient than in
Java. Ruby uses an internal hash to store the symbols and the lookup
is very, very fast. Ruby uses symbols internally for method lookup
and dispatch. In Java, as far as I can tell, the .intern() method is
an O(n) where n is the number of Strings that have been intern()ed.

Does this help?