I have a bunch of different .rb command line script files that perform various tasks, but they all use the same database.
I'm coding the 2nd .rb file, and the 1st one has all references to ActiveRecord, db connections, and classes that represent my models etc.
Is it possible to move all this to another file, and then just import the file in each of my .rb files?
How would thi开发者_开发百科s work?
require
basically goes out and runs another file in the context of the current ruby process if it hasn't be required yet. if your db file is called models.rb
and it is living in a sub directory called lib
, it would look like this
require 'lib/models'
You need to read Modules
, which is part of the "Pickaxe Book", AKA Programming Ruby
.
精彩评论