当文件不可用时,require家庭将抛出LoadError。此示例说明了仅在可选模块存在时加载它们。
module TidBits @@unavailableModules = [] [ { name: 'CoreExtend', file: 'core_extend/lib/core_extend' } \ , { name: 'Fs' , file: 'fs/lib/fs' } \ , { name: 'Options' , file: 'options/lib/options' } \ , { name: 'Susu' , file: 'susu/lib/susu' } \ ].each do |lib| begin require_relative lib[ :file ] rescue LoadError @@unavailableModules.push lib end end end # 模块TidBits