Subscribed unsubscribe Subscribe Subscribe

Kentaro Kuribayashi's blog

Software Engineering, Management, Books, and Daily Journal.

Released "MethodRepository": A Missing Part in OOP

MethodRepository is a library to extract redandunt code and commonalize it in a different way. You can get it via github/rubygems.

I'll explain my motivation why I wrote such a library.


To extract redandunt codes into a method to commonalize them is a usual strategy for OOP. It allows us to streamline our codes and modify at one stop even though the method is used at anywhere, anytimes.

I don't like when highly commonalized OOP structure disturbes me from quick tracing where such methods are defined. It's OOP's natural defect, I think. Once classes/modules are defined, it's inevitable that the classes/modules are inherited/included at anywhere we don't know.

In that way, inheritance/inclusion-based OOP resembles goto programming; There's no clear reason why some classes/modules are inherited/included by another classes/modules. Even though there's some structural thought in your classes/modules design, such an excessively free inheritance/inclusion prevent us from grasping the whole code quickily.


This library provides a "method repository" in which you can add your methods to commonalize redandunt codes here and there in your whole codes, which is just same as usual module usage. However, the methods you define in the "repository" will never be included automatically into other classes/modules unless not permitted explicitely.

This is the point; There's no chance the methods in the "repository" appear at somewhere the "repository" don't know. To commonalize redanduncy is our intension, but we don't want the methods to be used where we don't know. The way this library provides solves the problem.


Imagine there's such a code below:

module Repository
  include MethodRepository

  insert :method1, in: %w[Foo Bar] do; end
  insert :method2, in: %w[Baz]     do; end

class Foo; end
class Bar; end
class Baz; end
class Qux; end
  • method1 is declared it can be inserted in Foo and Bar
  • method2 is declared it can be inserted in only Baz
  • No method is declared it can be inserted in Qux


When the classes/objects are extended by Repository module:



foo =; foo.extend(Repository)
bar =; bar.extend(Repository)
baz =; bar.extend(Repository)
qux =; qux.extend(Repository)

Only explicitely permitted methods are defined as singleton methods of each classes/objects. That results in:

Foo.respond_to?(:method1) #=> true
Bar.respond_to?(:method1) #=> true
Baz.respond_to?(:method1) #=> false
Qux.respond_to?(:method1) #=> false

Foo.respond_to?(:method2) #=> false
Bar.respond_to?(:method2) #=> false
Baz.respond_to?(:method2) #=> true
Qux.respond_to?(:method2) #=> false


foo.respond_to?(:method1) #=> true
bar.respond_to?(:method1) #=> true
baz.respond_to?(:method1) #=> false
qux.respond_to?(:method1) #=> false

foo.respond_to?(:method2) #=> false
bar.respond_to?(:method2) #=> false
baz.respond_to?(:method2) #=> true
qux.respond_to?(:method2) #=> false


The rule is also applicable to include:

Foo.send(:include, Repository)
Bar.send(:include, Repository)
Baz.send(:include, Repository)
Qux.send(:include, Repository)

Results in: #=> true #=> true #=> false #=> false #=> false #=> false #=> true #=> false

In this case, the methods in Repository are, of course, defined as instance methods of each classes, not singleton methods of each objects.


This library solves a problem that OOP's inheritance is too free to design clearly understandable codes. It limits us to freely use commonalized methods from classes/modules. I think explicit declaration can help us to avoid such a goto-like chaos. I'll be very glad if you give me your opinion.