From: Robert Klemme Date: 2004-03-31T21:59:22+09:00 Subject: Re: How are Symbol objects used "gabriele renzi" schrieb im Newsbeitrag news:tncl60hesfj7dcfgl5b3ja6uio2ak95if2@4ax.com... > il Wed, 31 Mar 2004 23:23:46 +1200, "Robo" ha > scritto:: > > > Symbols are like Atoms in other languages -- they are invariant, and a > >> symbol only creates a new object if it's the first time it's used. > > > >I've been wondering about symbols as well. What are some examples of a good > >place to use symbols? > > when you use something related to reflection > (method(:sym) is an example) > > when you need a constant meaning something, insted of the C-like > Module::CONST > you can just use :const > :const is just like a number (immutable and so on) but it has the > plus have being meaningful. Yep. > in general, I even believe using symbols is safer than using string. > If you are using a string and do not need it's features (take a look > at String.instance_methods ) you may think of using a symbol (even if > not everything is allowed as a symbol), avoiding possibly messing up > something by changing the string. No, in that case you use #freeze. You should make it only dependent on the semantics and not on the methods you actually use. It's perfectly ok to have a string constant, when it's appropriate - even if you don't need to change it. Apart from that methods might not accept a Symbol. I'd always write File.open( "foo" ) {|io| ... } instead of File.open( :foo ) {|io| ... } whiel yields an invalid argument anyway. robert