OLW: Executing #Rails Scripts From IRB

This is a #ruby #one-liner if you need to execute an unknown script that requires a Rails context from a location that does not have access to a Rails context (e.g. a Rake task) and also need to retrieve a return value of that script, you can use a line as follows:

result = system "echo 'require \"#{path.to_s}\"' | rails c"

Here the path variable corresponds to the path to the ruby script you want to execute. The system command gives a return value based on if your script aborts or succeeds so you can check for failures.

“From now on, ending a sentence with a preposition is something up with which I will not put.” – Winston Churchill


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s