在Ruby中命名空间的首选方式(更好的样式)是什么?单数还是复数? [英] What is the preferred way (better style) to name a namespace in Ruby? Singular or Plural?

查看:77
本文介绍了在Ruby中命名空间的首选方式(更好的样式)是什么?单数还是复数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用哪种利弊?

FooLib::Plugins
FooLib::Plugins::Bar

vs.

FooLib::Plugin
FooLib::Plugin::Bar

命名约定?您将使用什么或正在使用什么?社区中最常用的是什么?

naming conventions? And what would you use or what are you using? What is more commonly used in the comunity?

推荐答案

使用:

module FooLib end
module FooLib::Plugins end
class  FooLib::Plugins::Plugin; end #the base for plugins
class  FooLib::Plugins::Bar < FooLib::Plugins::Plugin; end
class  FooLib::Plugins::Bar2 < FooLib::Plugins::Plugin; end

或用不同的词:

module FooLib
  module Plugins
    class Plugin; end #the base for plugins
    class Bar < Plugin; end
    class Bar2 < Plugin; end
  end
end

也可以这样排列文件:

- foo_lib/
  - plugins/
    - plugin.rb
    - bar.rb
    - bar2.rb

这是 Rails的实现方式(所以这就是Rails方式). IE.查看关联名称空间和 Associations :: Association类组成命名空间的所有类都从该类继承(即关联: SingularAssociation ).

This is how Rails does it (so this is the Rails Way). I.e. look at the Associations namespace and the Associations::Association class from which all of the classes form the Associations namespace inherits (i.e. Associations::SingularAssociation).

这篇关于在Ruby中命名空间的首选方式(更好的样式)是什么?单数还是复数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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