From: Devin Mullins Date: 2005-09-28T13:15:15+09:00 Subject: Re: Dynamically generating classes? Here's another way you might do it: ERROR_CODES = { :MyEx1 => {'code' => 1, 'message' => "Oops!"}, :MyEx2 => {'code' => 2, 'message' => "Whoops!"}, } #StandardError'd be the canonical one to inherit. #Exceptions outside of that tree are typically Ruby errors (bad syntax, no memory, etc.). class MyError < StandardError def to_s "{code: #{CODE}, message: #{MESSAGE}}" end end for k, v in ERROR_CODES Object.const_set(k, Class.new(MyError) { CODE = v['code'] MESSAGE = v['message'] }) end o = MyEx1.new p o __END__ # I think it's a little cleaner, but that's partially taste. Devin