From: Sean O'Halpin Date: 2005-09-29T00:53:31+09:00 Subject: Re: Dynamically generating classes? On 9/28/05, Jonathan Yurek wrote: > [snip] > > > # > > Isn't that wrong, though? You instantiated MyExt1, but your code and > message are displaying what MyExt2 should be. > > -- Jon > > Hey - you're right! It's the for vs each scoping again! Ouch! This works as expected: ERROR_CODES = { :MyEx1 => {'code' => 1, 'message' => "Oops!"}, :MyEx2 => {'code' => 2, 'message' => "Whoops!"}, } ERROR_CODES.each do |k,v| Object.const_set(k, Class.new(Exception) { define_method :to_s do "{code: #{v['code']}, message: #{v['message']}}" end }) end p MyEx1.new p MyEx2.new __END__ # # But best not to do it this way - see Devin Mullins' post for a better way. Sean