Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command line options

I am confused about Ruby command-line options. Both -C dir and -X dir remove directory, but how do they differ from each other?

  • How does -x [dir] differ from -X dir?
  • What does -I dir do (I know that it adds dir as the directory for loading libraries)?
like image 399
Arup Rakshit Avatar asked Feb 18 '23 21:02

Arup Rakshit


1 Answers

Let's create a test.rb file in home directory with following:

hello
#!/usr/bin/ruby
p "here"

Now if we try to run it:

ruby -C /home/my_home test.rb

Which means change working directory to /home/my_home and run test.rb you will get an error:

test.rb:1:in `<main>': undefined local variable or method `hello' for main:Object (NameError)

If we run it with:

ruby -x /home/my_home test.rb

We will get "here" printed and get no error. The main difference between -x and -C is that -x removes everything before the #!/usr/bin/ruby line. And you don't have to set directory to cd too, when using -x. Because the main purpose of -x is to remove lines and it just includes -C functionality too, if needed.

cd /home/my_home; ruby -x test.rb

See (ruby --help)

  • -Cdirectory cd to directory, before executing your script
  • -x[directory] strip off text before #!ruby line and perhaps cd to directory

As for -I. You can provide the directories that ruby will search for the file you execute or require.

ruby -x test.rb

Ruby will not find the test.rb file unless you are in /home/my_home. But if you add -I ruby will look for test.rb in "/home/my_home" too.

ruby -x -I/home/my_home test.rb

The difference with -C is that it will not change directory before executing, but will just search for files there.

like image 143
Draco Ater Avatar answered Feb 27 '23 17:02

Draco Ater