From: Brian Mitchell Date: 2007-12-13T23:52:17+09:00 Subject: Re: Is there any way to redefine the `new' method? On Dec 13, 2007 9:35 AM, pluskid wrote: > Hi! I'm writing a toy interpreter for scheme in Ruby. I created a > class to represent scheme Symbol. > And as the scheme Symbol, I want only one instance for symbols with > the same name. I try to > do it like this: > > class Symbol > alias orig_new new > def new(name) > @@symbols[name] ||= orig_new(name) > @@symbols[name] > end > end First of all, if you want a list or all symbols try: Symbol.all_symbols Next, if you want to define a method on the class object itself (i.e. a class method) the you should try this: class Symbol def Symbol.my_new(name) ... end end Symbol.my_new('foo') Now, lastly, you should note that new has no implementation on Symbol. This is deliberate. If you want to create a symbol from a string you should use the String#to_sym method: class Symbol def Symbol.my_new(name) name.to_sym end end If you want to get into the details of why all this is the way it is, then please ask. If you want to name my_new to new go ahead but I wouldn't count on it being called for symbols being created in literal style or via String#to_sym. Brian.