在几个文件中打破ruby模块

时间:2021-02-16 22:30:19

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

我有一个ruby模块,应该包含很多类

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?

我想在rails中做的是将这些类拆分成几个文件,将这个庞大的模块拆分成几个相关文件的最佳做法是什么?

1 个解决方案

#1


26  

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

#1


26  

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

相关文章