Hi,
I've implemented a DSL type system that uses instance_eval. I register properties in and then use method_missing to route the missing method calls through to the properties. My problem is that I want a property called 'test' but rather than go through to method_missing its calling Kernel.test (which I didn't know existed). Is there a way of making it so that the kernel methods are not visible when I call instance_eval? Or is there a way of creating a class such that it doesn't inherit Kernel?
Cheers,
James
Answering my own question. My DSL objects should subclass BasicObject.
···
From: ruby-talk [mailto:ruby-talk-bounces@ruby-lang.org] On Behalf Of James French
Sent: 09 April 2014 10:04
To: ruby-talk@ruby-lang.org
Subject: Hiding kernel methods from DSL
Hi,
I've implemented a DSL type system that uses instance_eval. I register properties in and then use method_missing to route the missing method calls through to the properties. My problem is that I want a property called 'test' but rather than go through to method_missing its calling Kernel.test (which I didn't know existed). Is there a way of making it so that the kernel methods are not visible when I call instance_eval? Or is there a way of creating a class such that it doesn't inherit Kernel?
Cheers,
James
That's what BasicObject is for:
2.0.0-p195 :025 > class MyObject < BasicObject
2.0.0-p195 :026?> def method_missing meth, *args, &blk
2.0.0-p195 :027?> ::Kernel.puts "#{meth} called with #{args}"
2.0.0-p195 :028?> end
2.0.0-p195 :029?> end
=> nil
2.0.0-p195 :030 > MyObject.new.hello
hello called with
=> nil
2.0.0-p195 :031 > MyObject.new.test
test called with
Jesus.
···
On Wed, Apr 9, 2014 at 11:04 AM, James French <James.French@naturalmotion.com> wrote:
Hi,
I've implemented a DSL type system that uses instance_eval. I register
properties in and then use method_missing to route the missing method calls
through to the properties. My problem is that I want a property called
'test' but rather than go through to method_missing its calling Kernel.test
(which I didn't know existed). Is there a way of making it so that the
kernel methods are not visible when I call instance_eval? Or is there a way
of creating a class such that it doesn't inherit Kernel?
Maybe you should try to undef Kernel#test in class where this methods are implemented, but I haven’t tried this idea, so this may not work, but why not to try?
···
--
Yuriy Plugatariov
Sent with Sparrow (http://www.sparrowmailapp.com/?sig\)
On Wednesday, April 9, 2014 at 12:04, James French wrote:
Hi,
I’ve implemented a DSL type system that uses instance_eval. I register properties in and then use method_missing to route the missing method calls through to the properties. My problem is that I want a property called ‘test’ but rather than go through to method_missing its calling Kernel.test (which I didn’t know existed). Is there a way of making it so that the kernel methods are not visible when I call instance_eval? Or is there a way of creating a class such that it doesn’t inherit Kernel?
Cheers,
James