DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Alexey has posted 28 posts at DZone. View Full User Profile

Inheriting Class And Instance Methods From Module To Class In Ruby

06.20.2010
| 3569 views |
  • submit to reddit
        Here you can see how to define both instance and class method in a module in order to mix them into a class


module MyModule
 def my_instance_method
  ...
 end

 def self.included klass
   def klass.my_class_method
     ...
   end
 end
end

And usage:
class MyClass
  include MyModule

  my_class_method
  ...
  def method_that_uses_the_module
    my_instance_method
    ...
  end
end