Selective module include

Howdy!

I’m inheriting from BasicObject (ruby 1.9), but in my case
I’d like it to be slightly less basic.

Currently I’m importing methods like:

def lambda(*args, &block)
Kernel.lambda(*args, &block)
end

def raise(*args)
Kernel.raise(*args)
end

def throw(*args)
Kernel.throw(*args)
end

But I’m guessing there’s probably a way I could do more
of a selective ‘include’ and import these methods more
directly, rather than wrapping them like this?

(I looked at the source for rb_include_module and
include_class_new, but it didn’t seem to involve any
loops that operated on a method-by-method basis.)

Thanks for any help,

Bill

On Wed, Oct 28, 2009 at 2:58 PM, Bill K. [email protected] wrote:

(I looked at the source for rb_include_module and
include_class_new, but it didn’t seem to involve any
loops that operated on a method-by-method basis.)

You may want to look at Daniel B.'s “use” library. Although I
cannot find it anywhere, he does mention it in ruby-talk:316199 and
includes the source code in the message.

http://www.nabble.com/Help,-Ruby-1.8.6-p287-broke-my-"use"--library-and-I-don't-know-why-td19706284.html

Best regards,
Michael G.

From: “Michael G.” [email protected]

You may want to look at Daniel B.'s “use” library. Although I
cannot find it anywhere, he does mention it in ruby-talk:316199 and
includes the source code in the message.

http://www.nabble.com/Help,-Ruby-1.8.6-p287-broke-my-"use"--library-and-I-don't-know-why-td19706284.html

Thanks! That got me started. I ended up with the following, which seems
to behave as I’d hoped.

class SpartanObject < BasicObject
include ::Kernel

keep = BasicObject.public_instance_methods +
BasicObject.protected_instance_methods +
BasicObject.private_instance_methods +
%w(
catch class inspect object_id raise throw to_s
is_a? kind_of? nil? respond_to?
).map {|m| m.to_sym}

(public_instance_methods +
protected_instance_methods +
private_instance_methods).each {|m| undef_method m unless
keep.include? m}
end

Regards,

Bill