将Ruby模块拆分成多个文件 [英] Breaking ruby module across several files

查看:67
本文介绍了将Ruby模块拆分成多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我想在rails中做的就是将这些类分解为几个文件,将这个庞大的模块分解为几个相关文件的最佳实践是什么?

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?

推荐答案

一种方法是提出这样的目录结构:

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

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

文件内容:

# 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

这篇关于将Ruby模块拆分成多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆