Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!news.musoftware.de!wum.musoftware.de!feeds.phibee-telecom.net!talisker.lacave.net!lacave.net!not-for-mail From: Andrew Berkeley Newsgroups: comp.lang.ruby Subject: class_eval doesn't find const_missing Date: Thu, 21 Apr 2011 14:20:46 -0500 Organization: Service de news de lacave.net Lines: 69 Message-ID: <094bd73f3389eb03672e8bb25c9b8ec6@ruby-forum.com> NNTP-Posting-Host: bristol.highgroove.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Trace: talisker.lacave.net 1303413663 81691 65.111.164.187 (21 Apr 2011 19:21:03 GMT) X-Complaints-To: abuse@lacave.net NNTP-Posting-Date: Thu, 21 Apr 2011 19:21:03 +0000 (UTC) X-Received-From: This message has been automatically forwarded from the ruby-talk mailing list by a gateway at comp.lang.ruby. If it is SPAM, it did not originate at comp.lang.ruby. Please report the original sender, and not us. Thanks! For more details about this gateway, please visit: http://blog.grayproductions.net/categories/the_gateway X-Mail-Count: 382019 X-Ml-Name: ruby-talk X-Rubymirror: Yes X-Ruby-Talk: <094bd73f3389eb03672e8bb25c9b8ec6@ruby-forum.com> Xref: x330-a1.tempe.blueboxinc.net comp.lang.ruby:3332 I am trying to write a DSL which uses method_missing and const_missing to catch upper and lowercase names (specified in isolation) and fetch the asscoated (already initialized) objects. This works great until I need to use class_eval which seemingly doesn't follow the custom const_missing method. Here's a more simple example of a class with a constant and #method_missing and #const_missing methods. class DummyClass NAME = 'dummy constant' def self.method_missing(method,*args,&block) return "this #{method} was captured by #method_missing" end def self.const_missing(constant) return "this #{constant} was captured by #const_missing" end end Accessing the constant is no problem > DummyClass::NAME => "dummy constant" Unknown methods and constants are handled as expected > DummyClass::ANOTHER_CONSTANT => "this ANOTHER_CONSTANT was captured by #const_missing" > DummyClass.some_method => "this some_method was captured by #method_missing" Accessing the constant via class_eval is variable - it works when evaluating strings but not blocks. > DummyClass.class_eval "NAME" => "dummy constant" > DummyClass.class_eval {NAME} NameError: uninitialized constant NAME from (irb):14 from (irb):14:in `class_eval' from (irb):14 from :0 This is partly understood since blocks are scoped to the context in which they were created. But even if the explicit constant is not recognised, why isn't the class #const_missing method invoked? Does the class_eval method allow the method_missing method to be used? > DummyClass.class_eval "dummy_method" => "this dummy_method was captured by #method_missing" > DummyClass.class_eval {dummy_method} => "this dummy_method was captured by #method_missing" Yes. #method_missing works as expected, in both string and block formats. So only #const_missing is causing a problem. Interestingly, an explicit call to #const_missing inside a class_eval block DOES work. > DummyClass.class_eval {const_missing :ANOTHER_CONSTANT} => "this ANOTHER_CONSTANT was captured by #const_missing" Anybody understand this, and is there a way to get around it? Cheers -- Posted via http://www.ruby-forum.com/.