Defining/nesting 个模块和 类 个应用程序 Rails
Defining/nesting modules and classes in a Rails app
我正在尝试在 Rails 应用程序中定义各种 modules/classes。我的目录结构如下所示:
lib/
fruit/ # just a module, with no associated file
fruit_operator.rb
apple.rb # abstract class, defines behavior for inheritance
orange.rb # abstract class, defines behavior for inheritance
apple/
granny_smith.rb # should inherit from apple.rb
red_delicious.rb
orange/
valencia.rb
seville.rb
我想要两件事:
- 子classes 应该从它们的父classes(Apple 和Orange)继承。
- 我应该能够从顶层访问这些 classes(在
/fruit
文件中——即 fruit_operator.rb
我为实现此目的所做的所有尝试都引发了某种错误。
尝试 # 1:
apple.rb
module Fruit
class Apple
def juicy
true
end
end
end
apple/granny_smith.rb
module Fruit
class GrannySmith::Apple
end
end
当我尝试从 fruit_operator.rb
访问 GrannySmith
时,我 运行 出错了。简单地访问 GrannySmith
生成
uninitialized constant Fruit::FruitOperator::GrannySmith
如果我尝试 Fruit::GrannySmith
,我会得到
uninitialized constant Fruit::GrannySmith
如果我尝试 Apple::GrannySmith
或 Fruit::Apple::GrannySmith
,我会遇到错误
Unable to autoload constant Fruit::Apple::GrannySmith, expected /lib/fruit/apple/granny_smith.rb to define it
尝试#2:
apple.rb
class Fruit::Apple
def juicy
true
end
end
apple/granny_smith.rb
class GrannySmith < Fruit::Apple
end
尝试从 fruit_operator.rb
访问时,我 运行 遇到与上述相同的错误。
尝试 #3:
apple.rb
class Fruit::Apple
def juicy
true
end
end
apple/granny_smith.rb
class Fruit::Apple::GrannySmith
end
最后一个版本允许我直接从 fruit_operator.rb
(如 Apple::GrannySmith
)访问 class,但它不继承自 Apple
!
知道如何 structure/access 这些 class 和模块吗?我环顾四周(在 SO 和其他地方),找不到关于如何做到这一点的好指南,特别是在 Rails 应用程序中。
您必须将水果文件的定义导入到水果算子文件中。例如,
require_relative './apple/granny_smith'
我认为您最好的解决方案是将 Fruit
实现为 class,并让 Apple
和 Orange
都继承自 Fruit
, GrannySmith
继承自 Apple
,像这样:
Class Fruit
def seeds?
true
end
end
Class Apple < Fruit
def juicy
true
end
end
class GrannySmith < Apple
def color
"green"
end
end
根据您对 fruit_operator
的需求,您可以选择 include
通过混合 Module
.
那些 methods/actions
我正在尝试在 Rails 应用程序中定义各种 modules/classes。我的目录结构如下所示:
lib/
fruit/ # just a module, with no associated file
fruit_operator.rb
apple.rb # abstract class, defines behavior for inheritance
orange.rb # abstract class, defines behavior for inheritance
apple/
granny_smith.rb # should inherit from apple.rb
red_delicious.rb
orange/
valencia.rb
seville.rb
我想要两件事:
- 子classes 应该从它们的父classes(Apple 和Orange)继承。
- 我应该能够从顶层访问这些 classes(在
/fruit
文件中——即fruit_operator.rb
我为实现此目的所做的所有尝试都引发了某种错误。
尝试 # 1:
apple.rb
module Fruit
class Apple
def juicy
true
end
end
end
apple/granny_smith.rb
module Fruit
class GrannySmith::Apple
end
end
当我尝试从 fruit_operator.rb
访问 GrannySmith
时,我 运行 出错了。简单地访问 GrannySmith
生成
uninitialized constant Fruit::FruitOperator::GrannySmith
如果我尝试 Fruit::GrannySmith
,我会得到
uninitialized constant Fruit::GrannySmith
如果我尝试 Apple::GrannySmith
或 Fruit::Apple::GrannySmith
,我会遇到错误
Unable to autoload constant Fruit::Apple::GrannySmith, expected /lib/fruit/apple/granny_smith.rb to define it
尝试#2:
apple.rb
class Fruit::Apple
def juicy
true
end
end
apple/granny_smith.rb
class GrannySmith < Fruit::Apple
end
尝试从 fruit_operator.rb
访问时,我 运行 遇到与上述相同的错误。
尝试 #3:
apple.rb
class Fruit::Apple
def juicy
true
end
end
apple/granny_smith.rb
class Fruit::Apple::GrannySmith
end
最后一个版本允许我直接从 fruit_operator.rb
(如 Apple::GrannySmith
)访问 class,但它不继承自 Apple
!
知道如何 structure/access 这些 class 和模块吗?我环顾四周(在 SO 和其他地方),找不到关于如何做到这一点的好指南,特别是在 Rails 应用程序中。
您必须将水果文件的定义导入到水果算子文件中。例如,
require_relative './apple/granny_smith'
我认为您最好的解决方案是将 Fruit
实现为 class,并让 Apple
和 Orange
都继承自 Fruit
, GrannySmith
继承自 Apple
,像这样:
Class Fruit
def seeds?
true
end
end
Class Apple < Fruit
def juicy
true
end
end
class GrannySmith < Apple
def color
"green"
end
end
根据您对 fruit_operator
的需求,您可以选择 include
通过混合 Module
.