Tuesday, September 11, 2012

Ruby - use include to append both class and instance methods

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


  def say_hello
    'instance hello'
  end
 
  module ClassMethods
    def say_hello
      'class hello'
    end 
  end 
end 

class A
  include B
end 

A.say_hello  # => "class hello"

A.new.say_hello # => "instance hello"

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.