Sam*_*ang 7 ruby api inheritance design-patterns composition
当我阅读有关Ruby元编程的更多信息时,大多数时候我们发现至少有两种解决方案来解决问题.请看下面两个例子:
class Base
def self.has_many(*args)
# ...
end
end
class Student < Base
has_many :books
end
Run Code Online (Sandbox Code Playgroud)
另一种风格:
module Base
def self.included(klass)
klass.extend ClassMethods
end
module ClassMethods
def has_many(*args)
# ...
end
end
end
class Student
include Base
has_many :books
end
Run Code Online (Sandbox Code Playgroud)
但是当我们设计api时,我们必须决定使用哪一个,但我想问一下大多数人已经在他们的库中实现的想法和一些最佳实践.
| 归档时间: |
|
| 查看次数: |
1051 次 |
| 最近记录: |