From: "Chr. Rippel" Date: 2002-01-09T04:00:18+09:00 Subject: Re: class name reported differently in different contexts "ts" wrote .... > Well, for me this is not a technical problem. > > ::inherited can raise an error and the class will never exist. > > Actually ruby give a classpath to a class when it is sure that the class > will exist, before this the class not yet exist and has only a classid Well, you could put the call to #inherited in a rescue clause and if necessary remove the newly created constant and re-raise the offending exception (see the ruby solution at the end). I am also not sure if I follow your and Matz logic on this matter completely. First of all anybody throwing an exception in a call to #inherited definitely deserves the pain to clean up the mess he/she probably created. Furthermore the current scheme does not protected you in a threaded enviroment and exceptions can occur at any time during class construction (which in Ruby are closer to class modification) anyway. --------- begin class A raise "tuff luck" if (rand(2) == 0) def bla; end end rescue RuntimeError => mes puts mes end --------- That said I am not particularly opinionated about this matter since I don't like the reliance on (constant)names as a programming technique in general. /Christoph --------- class Module private def correct_name (name,up = Object, &body) s = self begin if block_given? Class.new up do |k| s.const_set name, k end.class_eval(&body) else Class.new up do |k| s.const_set name, k end end rescue Exception => mes begin s.send :remove_const, name rescue NameError end raise mes end end end def correct_name (name,up = Object, &body) begin if block_given? Class.new up do |k| Object.const_set name, k end.class_eval(&body) else Class.new up do |k| Object.const_set name, k end end rescue Exception => mes begin Object.send :remove_const, name rescue NameError end raise mes end end # example class Base def Base.inherited(sub) puts "In #inherited, subclass name is #{sub}" raise "bad" if rand(2)==0 end end begin module Mod correct_name :Sub, Base do def bla; end puts "In class definition, subclass name is #{self}" end end rescue Exception => mes puts mes end begin correct_name :T, Base rescue Exception => mes puts mes end