What is the difference between :foo and "foo"?

Oh, I get it!!!
In see itwould be some_call(&@my_variable), and in ruby it's
some_call(:my_variable). One thing -- why not some_call(:@my_variable)?

No.

There is nothing even remotely close in C, C++, or Java.

In Ruby, it's *not* some_call(:my_variable), it's some_call(:my_name).

When you do:

   attr_accessor :my_name

You do NOT get a @my_name variable. You get two methods: Foo#my_name
and Foo#my_name= -- that's it. Consider:

class Foo
  attr_accessor :bar
end

=> nil

baz = Foo.new

=> #<Foo:0x2d8aea8>

Note. Thus far, there's no instance variable @bar on the Foo instance baz.

Foo.instance_methods(false)

=> ["bar", "bar="]

There's our instance methods.

baz.bar = 32

=> 32

baz

=> #<Foo:0x2d8aea8 @bar=32>

Now that we've called Foo#bar= on the baz instance of Foo class, baz
finally has a @bar instance variable. But not a moment before, unless
we instantiate such an instance variable prior to the call of
Foo#bar=.

So :bar is a name (Symbol) used to refer to the name :bar. It is used
by attr_accessor to create two methods that also operate on a
like-named instance variable. But :bar doesn't refer to a variable,
which is precisely why it isn't :@bar -- you're not creating a
variable @bar, you're creating instance methods #bar and #bar= that
happen to work on @bar in the instance.

-austin

···

On 28/12/05, Steve Litt <slitt@earthlink.net> wrote:
--
Austin Ziegler * halostatue@gmail.com
               * Alternate: austin@halostatue.ca

That's where the 'can be' part comes in :slight_smile:
The point is that symbols support quicker lookup by their nature.
Whether they are quicker in practice will depend on the
implementation. From the timings you give, it looks like symbol lookup
is implemented by converting the symbol to a string and doing string
lookup. Which is obviously not quicker :slight_smile:

My data were from a Common Lisp implementation, where symbols were
quicker in practice as well.
Sorry, didn't know about the Ruby implementation. Thanks for the info.

jf

···

On 12/28/05, ara.t.howard@noaa.gov <ara.t.howard@noaa.gov> wrote:

On Thu, 29 Dec 2005, Johannes Friestad wrote:

> Symbols are (or can be) quicker for hash lookup, since it is sufficient to
> compare object identity to find whether two symbols are the same, while
> strings must be compared character by character. You are unlikely to notice
> the difference unless your program uses hashes heavily.

i see this claim all the time but never data supporting it, all my test
programs have shown the opposite to be true. see

ara wrote:

i see this claim all the time but never data supporting it, all my test
programs have shown the opposite to be true.

My benchmark shows symbols to have a slight edge. I've attached my
benchmark at the end for review. (Benchmarks are tricky ... sometimes
you don't measure what you think you are measuring).

    $ ruby string_symbol_time.rb
    Strings Filling
     14.080000 0.090000 14.170000 ( 14.406058)
    Strings Fetching
      4.320000 0.030000 4.350000 ( 4.355025)

    Symbols Filling
     12.300000 0.030000 12.330000 ( 12.561648)
    Symbols Fetching
      3.370000 0.030000 3.400000 ( 3.461109)

also, don't forget that symbols are __never__ freed.

True, but when used properly, this is rarely a concern. If they are
used as programmer names for things, then the number of symbols is
finite and not likely to grow and consume memory as the program runs.

If however, you are dynamically creating symbols by interning strings, I
would suggest you review your use of symbols and consider using strings
instead.

···

--
-- Jim Weirich

-------------------------------------------------------------------
#!/usr/bin/env ruby

require 'benchmark'

SIZE = 100
N = 10000

def make_str_keys
  (1..SIZE).collect { |i| "key#{i}" }
end

def make_sym_keys(strs)
  strs.collect { |s| s.intern }
end

def populate(keys)
  result = {}
  keys.each_with_index do |k, i|
    result[k] = i
  end
  result
end

def fetch(keys, hash)
  keys.each do |key| hash[key] end
end

strs = make_str_keys
syms = make_sym_keys(strs)

str_hash = populate(strs)
sym_hash = populate(syms)

puts "Strings Filling"
puts Benchmark.measure {
  N.times do
    populate(strs)
  end
}

puts "Strings Fetching"
puts Benchmark.measure {
  N.times do
    fetch(strs, str_hash)
  end
}

puts
puts "Symbols Filling"
puts Benchmark.measure {
  N.times do
    populate(syms)
  end
}

puts "Symbols Fetching"
puts Benchmark.measure {
  N.times do
    fetch(syms, sym_hash)
  end
}

--
Posted via http://www.ruby-forum.com/\.

How about devoting the next Ruby Quiz to coming up with the best-of-class examples, self paced-tutorial and documentation to settle the :symbol vs "string" issue? At some point you have to ask yourself are the explanations given so far to inquiring users adequate. The fact that this question keeps coming up must be seen as evidence that there is something lacking in the explanations previously given. At a minimum all the explanations give so far should be edited up into a FAQ entry that the experts can agree upon. Just my two cents.
    6.1 What does :var mean? A colon followed by a name generates an integer(Fixnum) called a symbol which corresponds one to one with the identifier. "var".intern gives the same integer as :var, but the ``:'' form will create a local symbol if it doesn't already exist. The routines "catch", "throw", "autoload", and so on, require a string or a symbol as an argument. "method_missing", "method_added" and "singleton_method_added" (and others) require a symbol. The fact that a symbol springs into existence the first time it is referenced is sometimes used to assign unique values to constants: NORTH = :NORTH
SOUTH = :SOUTH
EAST = :EAST
WEST = :WEST

  http://www.rubycentral.com/faq/rubyfaqall.html#s6

···

---------------------------------
Yahoo! for Good - Make a difference this year.

Johannes Friestad wrote:
...

HOST = :host
PORT = :port
foo1 = {
    HOST => 'localhost',
    PORT => 80
}

which is equally safe, simpler to read, doesn't need the ConstUtil,
and automagically gives a constant with a readable string value.

My hack was a quicky to flesh out the example. After sending it I thought about assigning symbols. My main point was that if one mistypes a symbol name, Ruby doesn't care. Unit tests should catch this, but using constants might just help things along because of the immediate error. And it might more clearly express intent.

James

···

--

http://www.ruby-doc.org - Ruby Help & Documentation
Ruby Code & Style - Ruby Code & Style: Writers wanted
http://www.rubystuff.com - The Ruby Store for Ruby Stuff
http://www.jamesbritt.com - Playing with Better Toys
http://www.30secondrule.com - Building Better Tools

Along with Jim and Mauricio, my tests indicate that symbols are
consistently quicker, even on short strings.

Here's my benchmark

···

On 12/28/05, ara.t.howard@noaa.gov <ara.t.howard@noaa.gov> wrote:

On Thu, 29 Dec 2005, Johannes Friestad wrote:

> Symbols are (or can be) quicker for hash lookup, since it is sufficient to
> compare object identity to find whether two symbols are the same, while
> strings must be compared character by character. You are unlikely to notice
> the difference unless your program uses hashes heavily.

i see this claim all the time but never data supporting it, all my test
programs have shown the opposite to be true.

-------
  def bmark_string_symb
    require 'benchmark'
    strings, symbols=,
    n, m=100, 1000
    hash={}
    n.times {|x| strings<<strings<<x.to_s+"key"}
    strings.each {|s| symbols<<s.to_sym}
    # initialize hash
    strings.each {|s| hash[s]=1}
    symbols.each {|s| hash[s]=1}
    Benchmark.bm(10) do |b|
      b.report("string set") { m.times {|x| strings.each {|s| hash[s]=x}}}
      b.report("symbol set") { m.times {|x| symbols.each {|s| hash[s]=x}}}
      b.report("string get") { m.times {|x| strings.each {|s| hash[s]}}}
      b.report("symbol get") { m.times {|x| symbols.each {|s| hash[s]}}}
    end
  end
-------

and here are some results:
-------
irb(main):080:0> bmark_string_symb
                user system total real
string set 0.219000 0.016000 0.235000 ( 0.235000)
symbol set 0.141000 0.000000 0.141000 ( 0.141000)
string get 0.078000 0.000000 0.078000 ( 0.078000)
symbol get 0.047000 0.000000 0.047000 ( 0.047000)
=> true
=> true
irb(main):083:0> bmark_string_symb
                user system total real
string set 0.234000 0.000000 0.234000 ( 0.235000)
symbol set 0.063000 0.000000 0.063000 ( 0.062000)
string get 0.078000 0.000000 0.078000 ( 0.078000)
symbol get 0.047000 0.000000 0.047000 ( 0.047000)
=> true
-------

There's a fair amount of variation, but symbols appear to behave as
expected (quicker on average), meaning that my guess that symbol
lookup in hashes was done on the basis of their string value was
wrong.
I guess I should learn to refrain from speculating until I've checked closer :slight_smile:

jf

I still wonder if the lack of garbage collection of symbols
is merely a weakness in the current implementation, or something
that's part of the Ruby language.

Note that most lisps and javas can garbage collect
symbols and interned strings.

If it's just an implementation misfeature I'd love to
see that limitation go away in YARV.

Notes:
*1 http://community.schemewiki.org/?scheme-faq-language "Most Schemes
    do perform garbage-collection of symbols, since otherwise programs
    using string->symbol to dynamically create symbols would consume
    ever increasing amounts of memory even if the created symbols are
    no longer being used."

*2 interned Strings : Java Glossary "In the early JDKs, any
    string you interned could never be garbage collected because the
    JVM had to keep a reference to in its Hashtable so it could
    check each incoming string to see if it already had it in the
    pool. With JDK 1.2 came weak references. Now unused interned
    strings will be garbage collected."

···

ara.t.howard@noaa.gov wrote:

also, don't forget that symbols are __never__ freed.
this is a severe memory leak:

  loop{ Time::now.to_f.to_s.intern }

> As this thread quite well demonstrates,
> a definition for Symbols is quite difficult to come up with.

A formal definition is difficult to formulate correctly, but what CLHS
has is good enough:

"Symbols are used for their object identity to name various entities
in Common Lisp, including (but not limited to) linguistic entities
such as variables and functions." --
http://www.lisp.org/HyperSpec/Body/syscla_symbol.html#symbol

Ever since this discussion started, I've wondered if there was some
relationship between Ruby symbols and Lisp symbols. This is the first
thing I've seen that seemed to indicate that, though. Prior to this
quote you provided, everything that has been said in this discussion of
Ruby symbols has seemed to indicate that there is zero relationship
between the two.

So tell me: Is a Ruby symbol basically just an object oriented
implementation of the Lisp concept of a symbol? If that's the case,
what's up with all this tripe about "a symbol is just a name" and so on?
I get the distinct impression that either there's no real relationship
between Ruby symbols and Lisp symbols, or else all the people trying to
explain Ruby symbols don't much know what they're talking about.

It is just a way to name/identify things. In RL, you name/identify
that collection of atoms 'bird', and another collection of atoms
'cow'. In ruby, you name/identify an object 'host', and another object
'port'. Just as you name parts of collection of atom that you named
'bird' 'wings', 'legs', 'feathers', etc., you name parts of a
Hashtable that you named 'ServerConfig' 'host', 'port'.

Until you mentioned the hash table in that paragraph, it didn't make any
sense at all. I think these analogies are doing a disservice to people
trying to get a read on what a symbol is in Ruby. You might as well go
back to trying to explain OOP with truck analogies, which never worked
worth a damn for explaining the concept either, if you are going to do
that. It's impressively frustrating trying to make heads or tails of
this discussion of Ruby symbols: people are full of analogies and
categorical statements that don't really explain anything, but there
isn't a whole lot of substance to it.

If you tell me that Ruby symbols are basically just Lisp symbols,
implemented in an OOP way by way of a symbol class, I think I could
probably come up with a succinct and clear explanation of what a Ruby
symbol is that would remove a lot of the ambiguity, but so far I still
don't know if that's what a Ruby symbol actually is.

Just as one does not dumbed down "'everything' is an object in ruby",
which is a powerful ability, one should not dumbed down "symbols are
user-defined identifiers". It's just parts of learning curve.

Dumb down? No, never. Try explaining it, though. Identifier for what?
While you're at it, define "identifier" within this context.

···

On Thu, Dec 29, 2005 at 11:37:59PM +0900, Yohanes Santoso wrote:

--
Chad Perrin [ CCD CopyWrite | http://ccd.apotheon.org ]

"Real ugliness is not harsh-looking syntax, but having to
build programs out of the wrong concepts." - Paul Graham

Eero Saynatkari <ruby-forum-reg@mailinator.com> writes:

> Yohanes Santoso wrote:
>> Alex Knaub <aknaub@gmail.com> writes:
>>
>>>> Hi,
>>>>
>>>> I am a Ruby newbie. I wish I didn't post such a simple question here
>>>> but I had to.
>>>> What is the difference between :foo (a keyword) and "foo"(a string).
>>>> Can they be used interchangeably? Are they fundamentally same and is
>>>> the only difference performance?
>>>
>>> http://onestepback.org/index.cgi/Tech/Ruby/SymbolsAreNotImmutableStrings.red
>>
>> What a coincidence. Seems like Jim and I finally had enough of people
>> conflating symbols and immutable strings on the same day.
>>
>> http://microjet.ath.cx/WebWiki/2005.12.27_UsingSymbolsForTheWrongReason.html
>
> While, technically speaking, both of you are absolutely and
> undeniably correct (with one correction: Symbols are not Strings),
> such a conflation is the best way to get someone over the
> initial confusion.

The initial confusion is simply part of the learning curve rubyists
must go through along with learning that 'everything' is an
object. Dumbing down the situation can bring unexpected result:

Since we are around Christmas, I'm going to use Santa Claus as an
example. To prevent the initial confusion of children finding presents
magically appearing before Christmas tree, parents resort to such
outlandish notion that that is the work of a joyful guy who has never
heard of razor in a red jumper suit and riding reinders-pulled sleigh
around the world in one night.

Some children outgrew that notion. Some don't. Those that don't would
either sulk for a long time about the demise of such a joyful guy, or
start to think their parents are liars and spend the next untold years
of their life learning enough Math and Physics to prove that it is
impossible for a guy like Santa Claus to visit all children in one
night.

Yes, I like the rationale presented. Perhaps I should have worded
differently: conflating Symbols with immutable strings (not Strings)
makes it easier to understand their function in ruby. Any ambiguity
resulting from this is far easier to correct than it is the assumption
that Symbols are identifiers or references or variables (rather than
values, which is the first thing anyone needs to know about Symbols).

> As this thread quite well demonstrates,
> a definition for Symbols is quite difficult to come up with.

A formal definition is difficult to formulate correctly, but what CLHS
has is good enough:

"Symbols are used for their object identity to name various entities
in Common Lisp, including (but not limited to) linguistic entities
such as variables and functions." --
http://www.lisp.org/HyperSpec/Body/syscla_symbol.html#symbol

This makes no sense to someone just starting.

It is just a way to name/identify things. In RL, you name/identify
that collection of atoms 'bird', and another collection of atoms
'cow'. In ruby, you name/identify an object 'host', and another object
'port'. Just as you name parts of collection of atom that you named
'bird' 'wings', 'legs', 'feathers', etc., you name parts of a
Hashtable that you named 'ServerConfig' 'host', 'port'.

This makes sense but, as you notice, you are using 'strings' in
your explanation :slight_smile:

Just as one does not dumbed down "'everything' is an object in ruby",
which is a powerful ability, one should not dumbed down "symbols are
user-defined identifiers". It's just parts of learning curve.

Very good until this part. Symbols, most empathically, are *not*
identifiers (and I am quite sure this was merely a mistype/-thought
on your part).

> To paraphrase fifteen thousand fourty-three mediocre
> comedians over the last three centuries:
>
> "A Symbol is like a word, a sentence, a phrase, a
> description or, perhaps, a name. Except sometimes."

>
>> YS.

E

- --
"That sort of thing happens in bath tubs maybe. Not in real life."

···

On 2005.12.29 23:37, Yohanes Santoso <ysantoso-rubytalk@dessyku.is-a-geek.org> wrote:

>>> 2005/12/28, Surgeon <biyokuantum@gmail.com>:

That's what I suspected as shown in the comments of my last example, but Austin explains it much better. Learn something new all the time... :slight_smile:

James Edward Gray II

···

On Dec 28, 2005, at 2:51 PM, Austin Ziegler wrote:

When you do:

   attr_accessor :my_name

You do NOT get a @my_name variable. You get two methods: Foo#my_name
and Foo#my_name= -- that's it. Consider:

class Foo
  attr_accessor :bar
end

=> nil

baz = Foo.new

=> #<Foo:0x2d8aea8>

Note. Thus far, there's no instance variable @bar on the Foo instance baz.

Foo.instance_methods(false)

=> ["bar", "bar="]

There's our instance methods.

baz.bar = 32

=> 32

baz

=> #<Foo:0x2d8aea8 @bar=32>

Now that we've called Foo#bar= on the baz instance of Foo class, baz
finally has a @bar instance variable. But not a moment before, unless
we instantiate such an instance variable prior to the call of
Foo#bar=.

Depends on the call.
In the case of attr_*, it's because you're naming the attribute, not
the methods or the variable. The convention (and the code behind
attr_*) will do the expansion.
In instance_variable_get(...), you are explicitly looking for a
variable, and naturally supply the variable name.
In both cases, the symbol is just a name. What we are naming depends
on the context.

jf

···

On 12/28/05, James Edward Gray II <james@grayproductions.net> wrote:

On Dec 28, 2005, at 2:35 PM, Steve Litt wrote:

> One thing -- why not some_call(:@my_variable)?

This is a fair question I've asked myself once or twice. Ruby seems
to change it's mind on this sometimes too:

>> class MyClass
>> def initialize( var )
>> @var = var
>> end
>> attr_reader :var # I guess we're talking about the method here
(no @)
>> def fetch( name )
>> instance_variable_get("@#{name}") # but we need the @ now
>> end
>> end
=> nil
>> ex = MyClass.new(123)
=> #<MyClass:0x32565c @var=123>
>> ex.var
=> 123
>> ex.fetch(:var)
=> 123

James Edward Gray II

i never consider that as an impl - i bet your right though... time for me to
read the source.

cheers.

-a

···

On Thu, 29 Dec 2005, Johannes Friestad wrote:

That's where the 'can be' part comes in :slight_smile:
The point is that symbols support quicker lookup by their nature.
Whether they are quicker in practice will depend on the
implementation. From the timings you give, it looks like symbol lookup
is implemented by converting the symbol to a string and doing string
lookup. Which is obviously not quicker :slight_smile:

--

ara [dot] t [dot] howard [at] noaa [dot] gov
all happiness comes from the desire for others to be happy. all misery
comes from the desire for oneself to be happy.
-- bodhicaryavatara

===============================================================================

%w( NORTH SOUTH EAST WEST ).each{|c| const_set c, c}

:wink:

-a

···

On Thu, 29 Dec 2005, Dan Diebolt wrote:

NORTH = :NORTH
SOUTH = :SOUTH
EAST = :EAST
WEST = :WEST

--

ara [dot] t [dot] howard [at] noaa [dot] gov
all happiness comes from the desire for others to be happy. all misery
comes from the desire for oneself to be happy.
-- bodhicaryavatara

===============================================================================

Jim Weirich wrote:

also, don't forget that symbols are __never__ freed.

True, but when used properly, this is rarely a concern. If they are
used as programmer names for things, then the number of symbols is
finite and not likely to grow and consume memory as the program runs.

Here I am replying to my own posting, but I think this point could use
some elaboration.

Why are symbols not garbage collected? Because a symbol represents a
mapping from a string name to a unique object. Anytime in the execution
of the program, if that name is used for a symbol, the original symbol
object must be returned. If the symbol is garbage collected, then a
later reference to the symbol name will return a different object.
That's generally frowned upon (although I don't really see the harm. If
the original symbol was GC'ed, nobody cared what the original object was
anyways. But that's the way it works).

This might be one area where the "Symbol isa immutable string" meme
might be doing some real harm. We if think of symbols as strings, then
we tend to build symbols dynamically like we do strings. This is when
the memory leak" problem of symbols becomes a problem.

Here's a rule of thumb ... if a programmer never sees the symbol name in
the code base, then you probably should be using a string rather than a
symbol.

I'm not sure if this helped, or just muddied the water more.

···

--
-- Jim Weirich

--
Posted via http://www.ruby-forum.com/\.

BTW: Ruby version 1.8.2, Win XP Pro, Pentium M 2.0 GHz

jf

···

and here are some results:
...

<snip test>

that way well be true now. however, if you look at my test it goes to some
lengths to make the test a little more 'real-world':

   - creats a large (2 ** 13) hash
   - poplate using a semi-random distribution
   - selects keys for lookup in a semi-random distribution
   - fork for each test to isolate tests somewhat
   - disable GC for each test
   - runs each test 4 times

in anycase, all i'm driving at is that a pretty heavy duty test (not saying
mine is that test) is required to eliminate the differences data distribution,
gc, and system load have on the results. in particular i can see how a linear
distribution might have a huge effect - seeing as symbols are essentially
numbers and hash more predictably whereas making the jump from '9999' to
'10000' is likely to land in quite a different bucket.

it's nonetheless very interesting to see some tests though.

i use both btw. :wink:

-a

···

On Thu, 29 Dec 2005, Johannes Friestad wrote:

On 12/28/05, ara.t.howard@noaa.gov <ara.t.howard@noaa.gov> wrote:

On Thu, 29 Dec 2005, Johannes Friestad wrote:

Symbols are (or can be) quicker for hash lookup, since it is sufficient to
compare object identity to find whether two symbols are the same, while
strings must be compared character by character. You are unlikely to notice
the difference unless your program uses hashes heavily.

i see this claim all the time but never data supporting it, all my test
programs have shown the opposite to be true.

Along with Jim and Mauricio, my tests indicate that symbols are
consistently quicker, even on short strings.

--

ara [dot] t [dot] howard [at] noaa [dot] gov
all happiness comes from the desire for others to be happy. all misery
comes from the desire for oneself to be happy.
-- bodhicaryavatara

===============================================================================

Hmm, smells like work and documentation combined. Two evils in one quiz. :wink:

I suspect that would make for an unpopular topic.

James Edward Gray II

···

On Dec 28, 2005, at 4:18 PM, Dan Diebolt wrote:

How about devoting the next Ruby Quiz to coming up with the best-of-class examples, self paced-tutorial and documentation to settle the :symbol vs "string" issue?

Yes, I think Ruby symbols and Lisp symbols are the same thing. The
issue is just slightly obscured because Ruby glosses over the Lisp
concept of "quote" by building the quote syntax directly into the
symbol-literal syntax. :mysymbol means the same thing as Lisp's
'mysymbol, except the whole thing is treated as a single literal
token, instead of parsing to (quote mysymbol).

···

On Fri, Dec 30, 2005 at 01:50:14AM +0900, Chad Perrin wrote:

If you tell me that Ruby symbols are basically just Lisp symbols,
implemented in an OOP way by way of a symbol class, I think I could
probably come up with a succinct and clear explanation of what a Ruby
symbol is that would remove a lot of the ambiguity, but so far I still
don't know if that's what a Ruby symbol actually is.

Chad Perrin <perrin@apotheon.com> writes:

Prior to this quote you provided, everything that has been said in
this discussion of Ruby symbols has seemed to indicate that there is
zero relationship between the two.

The first line of my journal (third post in this thread) already
mentions "The concept of symbols have been popular among the lisp
community...".

So, there is not zero relationship between the two. Before coming to
ruby, I was already familiar with elisp and dabled some in CL. A quick
reading of the then scampy documentation of ruby Symbol revealed that
they are just different implementations of the same concept. But they
are not the same. ruby's symbol is CL&elips symbol minus attributes
(symbol value, property list, etc.)

So tell me: Is a Ruby symbol basically just an object oriented
implementation of the Lisp concept of a symbol?

Yes: different implementations of the same concept. No: CL&elisp's
Symbol has more functionalities.

If that's the case, what's up with all this tripe about "a symbol is
just a name" and so on?

Because it's true? Because there are people who don't have lisp
background who are not familiar with what symbol is? Because there are
people who thinks symbols are magical thingie?

I think these analogies are doing a disservice to people trying to
get a read on what a symbol is in Ruby.

This is true for all analogies. Different people are receptive to
different analogies. This simply means you are not receptive to the
analogy I made.

people are full of analogies and categorical statements
that don't really explain anything

The basic message has always been clear: symbol is a mean for you, the
programmer, to name/identify entities. But somehow, people are having
difficulty understanding this and asked for more explanations. This is
when analogies and categorical statements came in.

Just as one does not dumbed down "'everything' is an object in ruby",
which is a powerful ability, one should not dumbed down "symbols are
user-defined identifiers". It's just parts of learning curve.

Dumb down? No, never. Try explaining it, though.

As this and many other previous threads about symbol have shown,
explaining the concept of symbol is not easy.

While you're at it, define "identifier" within this context.

To help people understanding the meaning of 'identifier', I've been
mentioning it together with 'name': 'name/identify' as in "It is just
a way to name/identify things".

In any case, there is only one definition of identifier so context
should not influence the meaning of it:

From WordNet (r) 2.0 (August 2003) [wn]:

  identifier
      n : a symbol that establishes the identity of the one bearing it
          
YS.

Yes, the concept of a symbol in Ruby is the same as in Lisp. Just as
similar as the concepts for integers, strings, arrays and other basic
constructs.

I think the confusion comes from two areas:

1) Not many languages has 'symbol' as a distinct data type. Java has
interned strings, which are pretty close to symbols for most practical
purposes (symbols are atomic, immutable and unique, Java interned
strings are just immutable and unique), but they are not a separate
datatype.

2) It is not immediately obvious why symbols are useful. Numbers and
arrays/lists are easily explained as representing numbers and 'a
construct to hold multiple values', but there is no such simple
explanation for symbols. Not that I know of, anyway.

jf

···

On 12/29/05, Chad Perrin <perrin@apotheon.com> wrote:

Ever since this discussion started, I've wondered if there was some
relationship between Ruby symbols and Lisp symbols..
So tell me: Is a Ruby symbol basically just an object oriented
implementation of the Lisp concept of a symbol?