From: Day Date: 2008-02-06T11:39:56+09:00 Subject: Re: eval vs. class_eval On Feb 5, 2008 2:05 PM, Ryan Davis wrote: > You can cut out all the confusion by not evaling at all: > > > class Fil > def self.add_class name > const_set name.capitalize, Class.new(self) > end > end > > >> Fil.add_class "X" > => Fil::X > Yes, but that doesn't answer the question, really, about why the scope is so counter-intuitive. (Though this is handy and cleaner-looking, so I'm going to seriously consider using it, as long as it matches up with how we want this to work). I just hate to fix a problem and not know why it worked. I pared down the code we're puzzling over and pastied it: http://pastie.caboo.se/148064 I took out some logic (like converting the name of the new class to camel case), so this is sort of fragile, but I handed it only friendly values, so that should be alright. If you execute this as it, you'll see that the array returned by each of the three subtypes calls has the full chain of scope which, in this case, happens to mirror the inheritance. If you toggle off line 18 and on line 19, you'll see that everything seems to be scoped directly in Robert. I included lines 21 and 22 so that I could show that "self" has the same value with both eval and class_eval. So why is the scope different? As best I can tell, using const_set behaves like using class_eval. It may be that that's really what we want here, but still... What's up with eval? Thanks everyone for your help so far. And thanks in advance for further help. Ben