# Kernel.xxx interferes with method\_missing

**URL:** <https://www.ruby-forum.com/t/kernel-xxx-interferes-with-method-missing/159239>\
**Category:** Ruby\
**Created:** [February 2, 2009, 6:11pm UTC](https://www.ruby-forum.com/t/kernel-xxx-interferes-with-method-missing/159239 "2009-02-02T18:11:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![axyd80](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/82dd89/32.png) [@axyd80](https://www.ruby-forum.com/u/axyd80)\
**Post date:** [February 2, 2009, 6:11pm UTC](https://www.ruby-forum.com/t/kernel-xxx-interferes-with-method-missing/159239/1 "2009-02-02T18:11:47Z")

</div>

Hello!

I’m using method\_missing to create DSL, the problem is that Kernel.xxx  
methods are interferes with it.  
How this issue can be solved?

It seems that one solution is to explicitly define all global methods  
(p, select, …) on the target class for method\_missing.

But i hope there is more elegant solution :). Maybe some trick with  
sandboxes?

CODE:  
class Parameters # \< BlankSlate  
attr\_reader :result

```
def initialize &block
  @result = {}
  instance_eval &block
end

def method_missing m, *p
  @result[m] = *p
end

```

end

describe do  
it do  
hash = Parameters.new do  
one 1  
select1 ‘Select’ # \<= Problem here, it calls Kernel.select  
end.result

```
  hash.should == {:one => 1, :select1 => 'Select'}
end

```

end

---

<div class="post-metadata">

**Author:** ![axyd80](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/82dd89/32.png) [@axyd80](https://www.ruby-forum.com/u/axyd80)\
**Post date:** [February 2, 2009, 6:14pm UTC](https://www.ruby-forum.com/t/kernel-xxx-interferes-with-method-missing/159239/2 "2009-02-02T18:14:28Z")

</div>

Small error in code, ‘select’ not ‘select1’ :).

---

<div class="post-metadata">

**Author:** ![axyd80](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/82dd89/32.png) [@axyd80](https://www.ruby-forum.com/u/axyd80)\
**Post date:** [February 2, 2009, 6:57pm UTC](https://www.ruby-forum.com/t/kernel-xxx-interferes-with-method-missing/159239/3 "2009-02-02T18:57:39Z")

</div>

On Mon, Feb 2, 2009 at 12:11 PM, Alexey P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> sandboxes?  
> On Ruby 1.9, use BasicObject. On Ruby 1.8, use Jim W.'s  
> BlankSlate object. I recently ran into an issue with blankslate  
> though where it was incorrectly revealing methods, so you might want  
> to use my patched version:

[http://pastie.org/377487.txt](http://pastie.org/377487.txt)

Take this with a shaker of salt though, I mainly modified the example  
to show off certain dynamic programming techniques in my book[0], and  
not necessarily to make it robust. I have emailed Jim about these  
changes, but he hasn’t gotten back to me yet.

-greg

---

<div class="post-metadata">

**Author:** ![axyd80](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/82dd89/32.png) [@axyd80](https://www.ruby-forum.com/u/axyd80)\
**Post date:** [February 3, 2009, 7:32am UTC](https://www.ruby-forum.com/t/kernel-xxx-interferes-with-method-missing/159239/4 "2009-02-03T07:32:11Z")

</div>

Possible solution, a hack with redefining all global methods.

class ExtraBlankSlate \< BlankSlate  
CUSTOM\_UNDEFINE = [:p, :select, :puts]

```
undefine = Kernel.instance_methods + Object.instance_methods + 

```

CUSTOM\_UNDEFINE  
BlankSlate.instance\_methods.each{|m| undefine.delete m}

```
undefine.each do |m|
  script = %{\

```

def #{m} \*p, &b  
method\_missing :#{m}, \*p, &b  
end}  
class\_eval script, **FILE** , **LINE**  
end  
end

---

<div class="post-metadata">

**Author:** ![axyd80](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/82dd89/32.png) [@axyd80](https://www.ruby-forum.com/u/axyd80)\
**Post date:** [February 2, 2009, 7:01pm UTC](https://www.ruby-forum.com/t/kernel-xxx-interferes-with-method-missing/159239/5 "2009-02-02T19:01:10Z")

</div>

Whoops…

On Mon, Feb 2, 2009 at 12:56 PM, Gregory B.

> Take this with a shaker of salt though, I mainly modified the example  
> to show off certain dynamic programming techniques in my book[0], and  
> not necessarily to make it robust. I have emailed Jim about these  
> changes, but he hasn’t gotten back to me yet.

[0] [http://rubybestpractices.com](http://rubybestpractices.com)

Sorry for the shameless plug, but it’s directly relevant to the OP’s  
questions since I cover these topics in the book, and it will be under  
a creative commons license 9 months after it hits the shelves.

-greg
