From: John Johnson Date: 2003-06-02T00:13:21+09:00 Subject: Re: Advantages of Symbols over constants One of my concerns with symbols is they spring into exsistence on their own, ie. def foo(op) case op when :Bar puts "Bar" else puts "a bug!" end end foo(:bar) -- Regards, JJ Be Kind, Be Careful, Be Yourself on 6/1/03 9:51 AM, Jim Freeze at jim@freeze.org wrote: > On Sunday, 1 June 2003 at 22:00:23 +0900, Marek Janukowicz wrote: >> >> I wonder what are the advantages of using symbols over constants. For >> example: I want to define representation of SQL comparison operators. If >> I use constants - I have to define them, but I don't have to do explicit >> check when using them (because exception will be raised on non-existing >> constant). It is also easy to see which operators are defined when >> looking at the source code. Another advantage - constants have >> namespaces: they can be defined in the class scope. If I use symbols - I >> don't have to define them, but I need to verify whether the symbol is >> valid. I have also to write additional documentation to indicate valid >> Symbols for operators. >> >> I find kind of general tendency on this group to recommend using symbols >> rather then constants. From what I wrote above I'd rather use constants. >> Could you please explain me why should I use symbols? Am I missing some >> important symbols advantage? > > One advantage is that symbols are faster than constants. > Regarding the namespace issue, it may be ok to ask yourself > if it really is needed. It may be that if an invalid symbol > is passed, then it just falls off the end of some if/else or case > statement and throws an error. > If, on the other hand you can't do without the name space, create > a hash with the valid symbols as the key, and use #has_key? > to validate the symbol. >