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 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
--
CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
"The ability to quote is a serviceable
substitute for wit." - W. Somerset Maugham
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
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.
···
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
--
Keith Gaughan - kmgaughan@eircom.net - http://talideon.com/
Luck can't last a lifetime, unless you die young.
-- Russell Banks
"Ike" <rxv@hotmail.com> wrote in message
news:LQsyg.1261$0e5.1251@newsread4.news.pas.earthlink.net...
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
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").
···
On 7/28/06, Ike <rxv@hotmail.com> 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
===
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.
--
- Simen
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?
···
On 7/28/06, Ike <rxv@hotmail.com> 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
--
--------
David Pollak's Ruby Playground
http://dppruby.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.
Yep, that's about it, as far as I can tell.
"Tim Hunter" <sastph@sas.com> wrote in message
news:eado93$s4a$1@foggy.unx.sas.com...
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, IkeTokens 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
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?
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.
···
On Sat, Jul 29, 2006 at 04:18:28AM +0900, Matt Todd wrote:
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?
--
CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
Brian K. Reid: "In computer science, we stand on each other's feet."
Hi --
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.
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
You'll get different numbers.
David
···
On Sat, 29 Jul 2006, Matt Todd wrote:
--
http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <-----
http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
Ruby for Rails => book, Ruby for Rails
http://www.rubycentral.org => Ruby Central, Inc.
Hi --
"Tim Hunter" <sastph@sas.com> wrote in message
news:eado93$s4a$1@foggy.unx.sas.com...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, IkeTokens 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
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
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
···
On Sat, 29 Jul 2006, Ike wrote:
--
http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <-----
http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
Ruby for Rails => book, Ruby for Rails
http://www.rubycentral.org => Ruby Central, Inc.
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 9 Aug 2006, at 15:15, ngoc wrote:
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?
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 Thu, 10 Aug 2006, ngoc wrote:
> 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?
Hi --
···
On Sat, 29 Jul 2006, Chad Perrin wrote:
On Sat, Jul 29, 2006 at 04:18:28AM +0900, Matt Todd wrote:
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?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.).
David
--
http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <-----
http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
Ruby for Rails => book, Ruby for Rails
http://www.rubycentral.org => Ruby Central, Inc.
dblack@wobblini.net writes:
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? -IkeRuby 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.
···
On Sat, 29 Jul 2006, Ike wrote:
i like 'symbol literal' as in
array literal => [42]
hash literal => {42=>42}
symbol literal => :foo
2 cts.
-a
···
On Sat, 29 Jul 2006 dblack@wobblini.net wrote:
I'd just call them symbols, or Symbol objects (just as with strings, arrays,
etc.).
--
we can never obtain peace in the outer world until we make peace with
ourselves.
- h.h. the 14th dali lama
Even better -- more succinct, makes the same point. Thanks.
···
On Sat, Jul 29, 2006 at 05:07:16AM +0900, dblack@wobblini.net wrote:
On Sat, 29 Jul 2006, Chad Perrin 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.).
--
CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
unix virus: If you're using a unixlike OS, please forward
this to 20 others and erase your system partition.