From: dblack@... Date: 2006-03-26T22:59:10+09:00 Subject: Re: deciphering poignant guide chapter 4 example Hi -- On Sun, 26 Mar 2006, Simen wrote: > Symbols with the same values are the same objects, that is not the case > with strings. Symbols represents identifiers, strings values. For > example > > def method_missing( id, *args ) > puts id.class > end > asdf # undefined method > > Would output "Symbol", since the method identifier is indeed an > identifier. There's no constraint or direct cause-and-effect operating on Ruby here, though. For example, when you do: Array.instance_methods you get a list of strings, corresponding to method names. These strings identify the methods, as much as :asdf does in your example. > Symbols are simply names of stuff. Use them when you need to > name something whose value may change, but not whose semantics (ie > ary[:greeting] may be "Hello world" or "Hello johnny" but it's allways a > greeting. You shouldn't use Symbols for stuff you're going to output to > some io, for example, there's no point in doing > > puts :"Hello world" > > instead of > > puts "Hello world" I'd add that a lot of Ruby methods take strings or symbols: attr and friends, send, define_method, etc. So there's also no point doing: send(meth_name.to_sym) which one sees occasionally. David -- David A. Black (dblack@wobblini.net) Ruby Power and Light, LLC (http://www.rubypowerandlight.com) "Ruby for Rails" chapters now available from Manning Early Access Program! http://www.manning.com/books/black