Breaking ruby module across several files

落爺英雄遲暮 提交于 2019-12-03 15:54:25

问题


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


来源:https://stackoverflow.com/questions/12035057/breaking-ruby-module-across-several-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!