# Selective module include

**URL:** <https://www.ruby-forum.com/t/selective-module-include/178072>\
**Category:** Ruby\
**Created:** [October 28, 2009, 7:58pm UTC](https://www.ruby-forum.com/t/selective-module-include/178072 "2009-10-28T19:58:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kelleysislander](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kelleysislander](https://www.ruby-forum.com/u/kelleysislander)\
**Post date:** [October 28, 2009, 7:58pm UTC](https://www.ruby-forum.com/t/selective-module-include/178072/1 "2009-10-28T19:58:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kelleysislander](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kelleysislander](https://www.ruby-forum.com/u/kelleysislander)\
**Post date:** [October 29, 2009, 12:11am UTC](https://www.ruby-forum.com/t/selective-module-include/178072/2 "2009-10-29T00:11:46Z")

</div>

On Wed, Oct 28, 2009 at 2:58 PM, Bill K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](http://www.nabble.com/Help,-Ruby-1.8.6-p287-broke-my-%22use%22--library-and-I-don%27t-know-why-td19706284.html)

Best regards,  
Michael G.

---

<div class="post-metadata">

**Author:** ![kelleysislander](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kelleysislander](https://www.ruby-forum.com/u/kelleysislander)\
**Post date:** [October 29, 2009, 3:56am UTC](https://www.ruby-forum.com/t/selective-module-include/178072/3 "2009-10-29T03:56:31Z")

</div>

From: “Michael G.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> 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](http://www.nabble.com/Help,-Ruby-1.8.6-p287-broke-my-%22use%22--library-and-I-don%27t-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
