【发布时间】:2015-09-27 16:18:01
【问题描述】:
在我编写的一个 Ruby 程序中,我在“入口点”文件的顶部“需要”所有我需要的文件和模块。例如:
#Sets an absolute path for wherever the program is run from
#this_file = __FILE__
#BASEDIR = File.expand_path(File.join(this_file, '..'))
this_file = __FILE__
this_file_folder_nav = File.join(this_file, '..')
BASEDIR = File.expand_path(this_file_folder_nav)
#Required Gems
require 'ap'
require 'docx'
require 'sanitize'
etc
#Required files
require_relative "lib/commodity/stories.rb"
require_relative 'lib/worldgrowth/worldgrowth.rb'
require_relative "lib/prices/prices.rb"
require_relative 'lib/prices/prices_module.rb'
etc
I can access all the classes defined in the files above. And I can access classes defined in the 'stories.rb' in pirces_module.rb. All the required gems are accessible in all the files
问题:这是一种好的做法吗?这对我来说似乎很方便,我想在 node.js 中做同样的事情。
但是,我发现我必须在将使用该模块的所有文件上写 var module = require('someModule')。如果我有一个 node.js 应用程序的入口点文件,是否可以做类似于我在 Ruby 中所做的事情?
【问题讨论】: