Forum: Ruby Kernel.xxx interferes with method_missing

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
62711fa2787e85b5f0c88e245ef69f54?d=identicon&s=25 Alexey Petrushin (axyd80)
on 2009-02-02 18:11
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
62711fa2787e85b5f0c88e245ef69f54?d=identicon&s=25 Alexey Petrushin (axyd80)
on 2009-02-02 18:14
Small error in code, 'select' not 'select1' :).
31e038e4e9330f6c75ccfd1fca8010ee?d=identicon&s=25 Gregory Brown (Guest)
on 2009-02-02 18:57
(Received via mailing list)
On Mon, Feb 2, 2009 at 12:11 PM, Alexey Petrushin <axyd80@gmail.com>
wrote:
> sandboxes?
On Ruby 1.9, use BasicObject.  On Ruby 1.8, use Jim Weirich'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

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
31e038e4e9330f6c75ccfd1fca8010ee?d=identicon&s=25 Gregory Brown (Guest)
on 2009-02-02 19:01
(Received via mailing list)
Whoops...

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

> 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

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
62711fa2787e85b5f0c88e245ef69f54?d=identicon&s=25 Alexey Petrushin (axyd80)
on 2009-02-03 07:32
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
This topic is locked and can not be replied to.