[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.