General question about Ruby Eigenclass/Singleton

Posted by Dex on Stack Overflow See other posts from Stack Overflow or by Dex
Published on 2010-06-18T07:48:46Z Indexed on 2010/06/18 7:53 UTC
Read the original article Hit count: 172

Filed under:
|
|
module MyModule 
  def my_method; 'hello'; end
end

class MyClass 
  class << self
    include MyModule
  end 
end

MyClass.my_method   # => "hello

I'm unsure why "include MyModule" needs to be in the eigenclass in order to be called using just MyClass.

Why can't I go:

X = MyClass.new
X.my_method

How is this type of pattern useful if I can't call it from an instance?

© Stack Overflow or respective owner

Related posts about ruby

Related posts about singleton