Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass ruby script file to rails console

People also ask

How do I run a Ruby script from the shell?

In this case, it's a Ruby file to be executed with the Ruby interpreter. To mark the file as executable, run the command chmod +x test. rb. This will set a file permission bit indicating that the file is a program and that it can be run.

How do I run a Ruby script from the command line in Windows?

When you open the Command Prompt, you'll be in your home directory (usually C:\Users\yourname). So if your Ruby script is on your desktop, you'd type cd Desktop or C:\Users\yourname\Desktop and press Enter. Type ruby scriptname. rb and press ⏎ Return .

How do I make a Ruby script executable?

You can make the script executable with the following command: chmod +x hello. rb . chmod is a shell command that allows us to change the permissions for a file. The +x specifies that the script should be executable.


Actually, the simplest way is to run it with load inside the rails console

 load './path/to/foo.rb'

You can use

bundle exec rails runner "eval(File.read 'your_script.rb')"

UPDATE:

What we also have been using a lot lately is to load the rails environment from within the script itself. Consider doit.rb:

#!/usr/bin/env ruby

require "/path/to/rails_app/config/environment"

# ... do your stuff

This also works if the script or the current working directory are not within the rails app's directory.


In the meantime, this solution has been supported.

rails r PATH_TO_RUBY_FILE

Much simpler now.


script/console --irb=pry < test.rb > test.log

simple, dirty, and block the process at the end, but it does the job exactly like I wanted.