Yohanes Santoso wrote:
> Alex Knaub <aknaub / gmail.com> writes:
> 
> 
>>2005/12/28, Surgeon <biyokuantum / gmail.com>:
>>
>>>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


Question: Would using a constant be equally suitable for expressing 
intention, and (possibly) less error-prone?

# Assume ConstUtils.next_value
# ensures unique values
HOST = ConstUtils.next_value
PORT = ConstUtils.next_value

foo1 = {
    HOST => 'localhost',
    PORT => 80
}

A downside to using symbols as constants is that this will not raise any 
exceptions:


foo1 = {
    :hots => 'localhost',
    :prt => 80
}

But a typo in a constant will.


James
-- 

http://www.ruby-doc.org       - Ruby Help & Documentation
http://www.artima.com/rubycs/ - 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