# Rb\_include\_module() doesn't call Module#included

**URL:** <https://www.ruby-forum.com/t/rb-include-module-doesnt-call-module-included/149025>\
**Category:** Ruby\
**Created:** [September 19, 2008, 1:25pm UTC](https://www.ruby-forum.com/t/rb-include-module-doesnt-call-module-included/149025 "2008-09-19T13:25:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 19, 2008, 1:25pm UTC](https://www.ruby-forum.com/t/rb-include-module-doesnt-call-module-included/149025/1 "2008-09-19T13:25:39Z")

</div>

I’m writing a c extension and I want to create a class called “World”  
in C which includes a ruby module “Serializable”.

# autoexec.rb

module Serializable  
def self.included( otherMod )  
puts “Serializable is included by #{otherMod.name}”  
end  
end

// World.c  
…  
rb\_load( rb\_str\_new2(“autoexec.rb”), 0 );  
VALUE klass = rb\_define\_class(“World”, rb\_cObject);  
rb\_include\_module( klass, rb\_eval\_string(“Serializable”) );

# in IRB

> > require “World.so”  
> > =\> true
> 
> > World.included\_modules  
> > =\> [Serializable, Kernel]

As you can see, class “World” includes module “Serializable”, but  
Serializable#included is not called.  
I saw rb\_mod\_include (not rb\_include\_module) calls Module#included  
explicitly in eval.c, but it’s declared as a static function, so I  
can’t use it in my source code.

I think this problem is a bug, but how do you think ?

By the way, if I replace rb\_include\_module call with this,  
Module#included is called fortunately.

VALUE args [1] = { rb\_str\_new2(“include Serializable”) };  
rb\_mod\_module\_eval( 1, args, klass );

But I don’t want to use eval function because it’s relatively slow.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 19, 2008, 1:43pm UTC](https://www.ruby-forum.com/t/rb-include-module-doesnt-call-module-included/149025/2 "2008-09-19T13:43:56Z")

</div>

removed\_email\_address@domain.invalid wrote:

> By the way, if I replace rb\_include\_module call with this,  
> Module#included is called fortunately.
> 
> VALUE args [1] = { rb\_str\_new2(“include Serializable”) };  
> rb\_mod\_module\_eval( 1, args, klass );
> 
> But I don’t want to use eval function because it’s relatively slow.

You could use rb\_funcall instead.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 19, 2008, 2:31pm UTC](https://www.ruby-forum.com/t/rb-include-module-doesnt-call-module-included/149025/3 "2008-09-19T14:31:16Z")

</div>

unknown wrote:

> Tim H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > You could use rb\_funcall instead.
> 
> Thanks for your reply, Tim.  
> But I’m not sure you mean something like this :
> 
> ```
> VALUE args [1] = { rb_str_new2("Serializable") };
> rb_funcall( klass, rb_intern("include"), 1, args );
> 
> ```
> 
> Am I correct ?

The argument to include is a module, not a string, so you’ll need to  
pass the Serializable module VALUE instead of the string “Serializable”.  
If you’ve defined Serializable in C, then that would be the return value  
from rb\_define\_module\_under (or whatever function you called to define  
Serializable). If you’ve defined Serializable in Ruby, then you’ll have  
to call rb\_const\_get get the value of Serializable.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 19, 2008, 2:40pm UTC](https://www.ruby-forum.com/t/rb-include-module-doesnt-call-module-included/149025/4 "2008-09-19T14:40:51Z")

</div>

Thanks for you detailed explanation, Tim.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 19, 2008, 2:10pm UTC](https://www.ruby-forum.com/t/rb-include-module-doesnt-call-module-included/149025/5 "2008-09-19T14:10:55Z")

</div>

Tim H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> You could use rb\_funcall instead.

Thanks for your reply, Tim.  
But I’m not sure you mean something like this :

```
VALUE args [1] = { rb_str_new2("Serializable") };
rb_funcall( klass, rb_intern("include"), 1, args );

```

Am I correct ?
