From: lasitha Date: 2009-03-29T13:54:23+09:00 Subject: Re: Dynamic object creation in ruby On Sun, Mar 29, 2009 at 9:00 AM, Clash Fasn wrote: > I'm trying to do dynamic object creation in ruby. > > This works: > > john = eval("Master::Person").new("John") > > This fails: > > frank = Object.const_get("Master::Person").new("Frank") > > Does anyone know why the second way fails? > -- > Posted via http://www.ruby-forum.com/. This will work: Object.const_get("Master")::Person.new("Frank") Or: Object.const_get("Master").const_get("Person").new("Frank") Or: "Master::Person".split('::').reduce(Object){|cls, c| cls.const_get(c) } But your question was 'why?'... I guess the short answer is #const_get just doesn't interpret the scope operator, '::'. Actually, thinking of it as an operator (hence it works in eval) and not a naming convention is perhaps helpful. cheers, lasitha