Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass an argument when calling a script in Rails console with load command

I know about accessing the command arguments using the ARGV array, but I've run into an issue. I have a script that I cannot run standalone and instead needs to be run in the rails console. Is there a way to pass arguments when calling a file as such?

load '/tmp/test.rb'

I tried placing it inside the quotes, outside and on a whim tried < to no avail.

Thank you for any help you can provide!

like image 666
SnakeMan2058 Avatar asked Jan 19 '26 16:01

SnakeMan2058


1 Answers

It is dirty hack but seems like you can assign array to ARGV and use it from loaded scripts as you wanted in question:

$  Temp  cat argv.rb
p ARGV
$  Temp  irb
2.1.0 :001 > ARGV
 => []
2.1.0 :002 > load 'argv.rb'
[]
 => true
2.1.0 :003 > ARGV = ['A', 'B']
(irb):3: warning: already initialized constant ARGV
 => ["A", "B"]
2.1.0 :004 > load 'argv.rb'
["A", "B"]
 => true
2.1.0 :005 >
like image 102
Maxim Avatar answered Jan 21 '26 08:01

Maxim