How to extend class with the particular method

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

On 11/1/07, Nikolay P. [email protected] 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
If you want to check out traits here is the link anyway:
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

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

That was extremely helpfull. Thanks :slight_smile:

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