您可以通过一个 require 语句来 require 一整套模块或 类 吗?
Can you require a whole set of modules or classes via one require statement?
我正在尝试使用一个 require
语句导入许多 classes。这可能吗?
我有以下文件结构:
在我的 main.cr
中,我有以下内容:
require "./module-a"
require "./module-b"
require "./module-c"
ModuleA.who_am_i()
ModuleB.who_am_i()
instanceB = ClassB.new
instanceB.who_am_i()
ModuleC.who_am_i()
instanceC = ClassC.new
instanceC.who_am_i()
当我运行这个程序时,我得到以下信息:
Showing last frame. Use --error-trace for full trace.
In main.cr:9:13
9 | instanceB = ClassB.new
^
Error: undefined constant ClassB
Did you mean 'Class'?
exit status 1
ClassB
在文件 module-b/module-b.cr
中定义。修复方法是将 require 语句更改为 ./module-b/*
。对于 module-c
,我在其自己的文件中定义了 ClassC
。有没有办法只用 1 个 require 语句导入所有 classes?还是我必须指定我想要的每个 class?
我在想是否有一个 javascript 等价物,我在一个文件夹中定义了 1 个 index.js
文件,该文件执行多个 modules.export
语句来导出所有函数和 classes.
以下回复描述了整个设置...
感谢您的帮助!
感谢 Discord 上的 Crystal 编程社区(值得加入 - 感谢来自服务器的 Blacksmoke16 和 jack arthur),我能够想出这个解决方案:
main.cr
require "./**"
ModuleA.who_am_i()
ModuleB.who_am_i()
instanceB = ModuleB::ClassB.new
instanceB.who_am_i()
ModuleC.who_am_i()
instanceC = ModuleC::ClassC.new
instanceC.who_am_i()
虽然它不如 Node 优雅,但也能胜任。
感谢@matthewm(在 Discord 服务器上):
This solution is requiring all the files and nested files starting
from the directory the file with the requires is in. One problem you
might run into with doing only that is that you are defaulting to the
require order of whatever crystal chooses. If you have a module_c that
depends on module_b but you do require ./**, Crystal might try to
require module_c before module_b and it will fail so you will need
either always require files directly and in the correct order or
require files individually before requiring everything at once:
require "./module_b"
require "./**" # files in here depend on module_b to be loaded first
我正在尝试使用一个 require
语句导入许多 classes。这可能吗?
我有以下文件结构:
在我的 main.cr
中,我有以下内容:
require "./module-a"
require "./module-b"
require "./module-c"
ModuleA.who_am_i()
ModuleB.who_am_i()
instanceB = ClassB.new
instanceB.who_am_i()
ModuleC.who_am_i()
instanceC = ClassC.new
instanceC.who_am_i()
当我运行这个程序时,我得到以下信息:
Showing last frame. Use --error-trace for full trace.
In main.cr:9:13
9 | instanceB = ClassB.new
^
Error: undefined constant ClassB
Did you mean 'Class'?
exit status 1
ClassB
在文件 module-b/module-b.cr
中定义。修复方法是将 require 语句更改为 ./module-b/*
。对于 module-c
,我在其自己的文件中定义了 ClassC
。有没有办法只用 1 个 require 语句导入所有 classes?还是我必须指定我想要的每个 class?
我在想是否有一个 javascript 等价物,我在一个文件夹中定义了 1 个 index.js
文件,该文件执行多个 modules.export
语句来导出所有函数和 classes.
以下回复描述了整个设置...
感谢您的帮助!
感谢 Discord 上的 Crystal 编程社区(值得加入 - 感谢来自服务器的 Blacksmoke16 和 jack arthur),我能够想出这个解决方案:
main.cr
require "./**"
ModuleA.who_am_i()
ModuleB.who_am_i()
instanceB = ModuleB::ClassB.new
instanceB.who_am_i()
ModuleC.who_am_i()
instanceC = ModuleC::ClassC.new
instanceC.who_am_i()
虽然它不如 Node 优雅,但也能胜任。
感谢@matthewm(在 Discord 服务器上):
This solution is requiring all the files and nested files starting from the directory the file with the requires is in. One problem you might run into with doing only that is that you are defaulting to the require order of whatever crystal chooses. If you have a module_c that depends on module_b but you do require ./**, Crystal might try to require module_c before module_b and it will fail so you will need either always require files directly and in the correct order or require files individually before requiring everything at once:
require "./module_b"
require "./**" # files in here depend on module_b to be loaded first