2

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!

SnakeMan2058
  • 77
  • 1
  • 11
  • Could you please explain why you can't run it standalone? Seems like the problem in this. – Maxim Feb 26 '15 at 17:37
  • Its a script to export objects and attachments from my rails instance, and needs to reference and search for objects. – SnakeMan2058 Feb 26 '15 at 18:07

2 Answers2

5

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 >
Maxim
  • 9,701
  • 5
  • 60
  • 108
4

You can do it like this:

bundle exec rails runner /tmp/test.rb argument1 argument2
actaram
  • 2,038
  • 4
  • 28
  • 49
  • Thanks, this makes sense but my console takes quite awhile to load. Im only using the arguments to prevent having to manually edit the input file variable in the script save it and load it again. Manually making this change is ultimately faster than loading the console each time. – SnakeMan2058 Feb 26 '15 at 18:05
  • 3
    For future viewers this is the technically correct answer, but the other is more useful for my situation – SnakeMan2058 Feb 26 '15 at 18:05
  • how do you catch these parameters? `params` or `parameters`... or how do you code test.rb to catch "argument1" and "argument2" to a two variables? – Albert Català Dec 11 '17 at 14:29
  • @AlbertCatalà In your ruby script, you can access all your arguments with the `ARGV` special variable. `ARGV` is an array containing all the arguments, as strings, that were passed to your script from the command-line. – actaram Dec 11 '17 at 15:06
  • Thanks , finally I've found it – Albert Català Dec 11 '17 at 15:19