# How to extend class with the particular method

**URL:** <https://www.ruby-forum.com/t/how-to-extend-class-with-the-particular-method/118933>\
**Category:** Ruby\
**Created:** [November 1, 2007, 2:57pm UTC](https://www.ruby-forum.com/t/how-to-extend-class-with-the-particular-method/118933 "2007-11-01T14:57:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nikolay\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f9ae1b/32.png) [@Nikolay\_P](https://www.ruby-forum.com/u/Nikolay_P)\
**Post date:** [November 1, 2007, 2:57pm UTC](https://www.ruby-forum.com/t/how-to-extend-class-with-the-particular-method/118933/1 "2007-11-01T14:57:23Z")

</div>

Hello.  
I have a class and a module. I want to extend a class with a particular  
method that is defined in a module. I have come up to the following  
example code, but i am not sure how to implement a  
Operational::Behavior#proc method. Could some one please help me in  
this?  
Or may be prompt a better implementation?

====================================================  
class SomeClass  
include Operational

operational\_as :bob  
end

module Operational  
def self.included(base)  
base.extend ClassMethods  
end

module ClassMethods  
def operational\_as(method)  
klass = class \<\< self; self; end  
klass.instance\_eval do  
define\_method(method.to\_sym, Behavior.proc(method.to\_sym))  
end  
end  
end

module Behavior

```
def self.proc(method)
end

private

def bob

```

…  
end

```
def sam

```

# … end end end

---

<div class="post-metadata">

**Author:** ![Nikolay\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f9ae1b/32.png) [@Nikolay\_P](https://www.ruby-forum.com/u/Nikolay_P)\
**Post date:** [November 1, 2007, 4:47pm UTC](https://www.ruby-forum.com/t/how-to-extend-class-with-the-particular-method/118933/2 "2007-11-01T16:47:55Z")

</div>

On 11/1/07, Nikolay P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> def operational_as(method)
> end
> 
> ```
> 
> # end end
> 
> # –
> 
> - Best regards, Nikolay P… \<\<\<-----------------------------------  
> ======================================================================

Hmm I am not 100% sure what you want exactly  
if I understand correctly you want to include a single method of a  
module into whatever.  
I have implemented this in traits but the overhead is big, you can  
however see in the code  
how this is done.  
Maybe it is worthy to look into the “use” package, written by Daniel.  
It does _exactly_ and _only_ that.This has been discussed in the  
original announcement of the traits  
[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/274261](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/274261)  
If you want to check out traits here is the link anyway:  
[http://rubyforge.org/frs/?group\_id=4642&release\_id=15737](http://rubyforge.org/frs/?group_id=4642&release_id=15737)

Ok maybe an example would be in order, just to be sure that it does  
what you want  
before digging into the code.

require ‘pure-traits’

T = trait {  
def a; 42 end  
def b; 222 end  
}

class C  
class \<\< self  
use T & [:a]  
end  
end

puts C.a  
puts C.b

HTH  
Robert

---

<div class="post-metadata">

**Author:** ![Nikolay\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f9ae1b/32.png) [@Nikolay\_P](https://www.ruby-forum.com/u/Nikolay_P)\
**Post date:** [November 1, 2007, 6:53pm UTC](https://www.ruby-forum.com/t/how-to-extend-class-with-the-particular-method/118933/3 "2007-11-01T18:53:46Z")

</div>

On Thursday 01 November 2007 17:47:06 Robert D. wrote:

> Hmm I am not 100% sure what you want exactly  
> if I understand correctly you want to include a single method of a  
> module into whatever.  
> I have implemented this in traits but the overhead is big, you can  
> however see in the code  
> how this is done.  
> Maybe it is worthy to look into the “use” package, written by Daniel.  
> It does _exactly_ and _only_ that.This has been discussed in the  
> original announcement of the traits  
> [http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/274261](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/274261)

That was extremely helpfull. Thanks 🙂

irb(main):032:0\> require ‘use’  
=\> true  
irb(main):033:0\> module Operational  
irb(main):034:1\>  
irb(main):035:1\* def self.included(base)  
irb(main):036:2\> base.extend ClassMethods  
irb(main):037:2\> end  
irb(main):038:1\>  
irb(main):039:1\* module ClassMethods  
irb(main):040:2\> def operational\_as(method\_name, options = {})  
irb(main):041:3\> self.instance\_eval do  
irb(main):042:4\* use Behavior, method\_name  
irb(main):043:4\> end  
irb(main):044:3\> end  
irb(main):045:2\> end  
irb(main):046:1\> module Behavior  
irb(main):047:2\> def bob  
irb(main):048:3\> end  
irb(main):049:2\> def sam  
irb(main):050:3\> end  
irb(main):051:2\> end  
irb(main):052:1\> end  
=\> nil  
irb(main):053:0\> class Op  
irb(main):054:1\> include Operational  
irb(main):055:1\> end  
=\> Op  
irb(main):058:0\> @phantomas = Op.new  
=\> #Op:0xb7a56484  
irb(main):056:0\> Op.operational\_as(:bob)  
=\> Op  
irb(main):060:0\> @phantomas.respond\_to?(:bob)  
=\> true  
irb(main):061:0\> @phantomas.respond\_to?(:sam)  
=\> false  
irb(main):062:0\> Op.operational\_as(:sam)  
=\> Op  
irb(main):063:0\> @phantomas.respond\_to?(:bob)  
=\> false  
irb(main):064:0\> @phantomas.respond\_to?(:sam)  
=\> true
