[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

:identifier

ashishwave

4/25/2006 7:13:00 AM

what is the meaning of : (colon) in the :identifier.

atmost, what i could find is in the "symbol" section of
http://www.poignantguide.net/ruby/chap.... But could not get it.
What is the difference with of this syntax with normal variable or
string etc. What is he advantage of using it.
bye :-)
Ashish
ashishwave@yahoo.com

4 Answers

Robert Klemme

4/25/2006 8:51:00 AM

0

ashishwave wrote:
> what is the meaning of : (colon) in the :identifier.
>
> atmost, what i could find is in the "symbol" section of
> http://www.poignantguide.net/ruby/chap.... But could not get it.

A symbol is a unique immutable representation of a string:

irb(main):002:0> :foo.object_id
=> 3741966
irb(main):003:0> :foo.object_id
=> 3741966
irb(main):004:0> :foo.object_id
=> 3741966
irb(main):005:0> "foo".object_id
=> 2016676
irb(main):006:0> "foo".object_id
=> 2000380
irb(main):007:0> "foo".object_id
=> 1989268

It's main use is as an identifier. For example with method send. Also,
you'll often see it as a hash key.

> What is the difference with of this syntax with normal variable or
> string etc. What is he advantage of using it.

There have been many discussions about Symbols and their usage. You'll
find them quite easily by searching the archives of c.l.r and ruby-talk.

Kind regards

robert

ashishwave

4/25/2006 10:27:00 AM

0

Thanks for such a nice , but clear example. got it.

Explanation by Markus at following url was also helpful.
http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/874e59886ca1270a/a92df80f3d08c0cd?q=%3ASymbols&rnum=9#a92df8...

bye :-)
Ashish

Gregor Kopp

4/26/2006 8:12:00 AM

0

If you use symbols where no changes on the strings are neccessary (like
hashkeys, and so on) you got a little speedup in processing.

Look at this, i found on www.rubyforen.de:

require 'benchmark'
h=Hash.new
Benchmark.bm do |x|
x.report("symbol"){ 1000000.times do |a|
h[:a]="gruen"<<a.to_s
result=h[:a]
end
}
x.report("string"){ 1000000.times do |a|
h["a"]="gruen"<<a.to_s
result=h["a"]
end
}

x.report("newElement"){ 1000000.times do |a|
h[a]="gruen"<<a.to_s
result=h[a]
end
}
end


produces:

user system total real
symbol 3.920000 0.000000 3.920000 ( 3.931316)
string 5.170000 0.010000 5.180000 ( 5.192685)
newElement 9.110000 0.100000 9.210000 ( 9.231569)

ashishwave schrieb:
> what is the meaning of : (colon) in the :identifier.
>
> atmost, what i could find is in the "symbol" section of
> http://www.poignantguide.net/ruby/chap.... But could not get it.
> What is the difference with of this syntax with normal variable or
> string etc. What is he advantage of using it.
> bye :-)
> Ashish
> ashishwave@yahoo.com
>

sabat

4/27/2006 2:46:00 AM

0

This URL has the best explanation I've found so far. I think I actually
(kind of) get it!

http://glu.ttono.us/articles/2005/08/19/understanding-ru...