views:

1413

answers:

2

Rails provides a very useful console ('script/console').

I write a ruby file and run it in the console using require foo.rb.

It works fine the first time, but the second and next require foo.rb does not run my script (require does not reload it).

Do you have any tips/tricks?

A: 

You should probably try either loading your rails environment in a script or using rake. Also consider using script/runner.

Here is an old and possibly outdated example of using your rails environment in a script. A more recent and detailed version here.

A stack overflow answer

srboisvert
+4  A: 

require is used to load extensions - so the code will execute once, to get the extensions to be present in your environment, but subsequent requires won't do anything, because the job has already been done.

load, on the other hand, loads and executes the code every time.

As already mentioned, if you just want to run your script and you need the Rails environment, then consider using script/runner

Mike Woodhouse
Make sure you use the full file name with load (require doesn't require this ;))
bandhunt