DEV Community

loading...

Discussion on: My Journey into Ruby: Modules

Collapse
topofocus profile image
Hartmut B.

Nice introduction.

Modules can be used to create real singletons in ruby.
from github.com/ib-ruby/ib-symbols/blob... :

module  IB
  module Symbols
    def self.allocate_m name 
      m= Module.new do
            extend Symbols
            (..)
            def self.yml_file    (...)  end
        end   # Module new             
        (..)
      name =  name.to_s.camelize.to_sym
      Symbols.const_set  name, m    # generate the module-const
    end
  end
end
Enter fullscreen mode Exit fullscreen mode

Then
IB::Symbols.allocate_m :c
creates the module IB::Symbols::Cand the defined methods.

No class is involved in the hole approach .

IB::Symbols::C.yml_file is a real singleton.