From: Joel VanderWerf Date: 2003-06-02T00:21:02+09:00 Subject: Re: Advantages of Symbols over constants 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 of symbols is that they know how to print themselves, but you can of course get this advantage with constants that refer to symbols: UP = :up DOWN = :down x = UP puts "You are going #{x}" Another advantage with constants is that, if you want to change the value, there's only one spot in the code to change and you don't break the API.