Breaking ruby module across several files

2019-03-17 11:57发布

问题:

I have a ruby module that is supposed to wrap up quite a few classes

module A
  class First
    #somemethods
  end

  class Second
    #somemethods
  end

  class Third
    #somemethods
  end
end

What i would like to do in rails is to break up these classes into several files what might be the best practice to split this huge module into several relevant files?

回答1:

One approach would be to come up with directory structure like this:

(root dir)
├── a
│   ├── first.rb
│   ├── second.rb
│   └── third.rb
└── a.rb

Files contents:

# a.rb
require_relative './a/first.rb'
require_relative './a/second.rb'
require_relative './a/third.rb'

module A
end


# a/first.rb
module A
  class First
    # ...
  end
end


# a/second.rb
module A
  class Second
    # ...
  end
end


# a/third.rb
module A
  class Third
    # ...
  end
end