Command line options

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

  • How is -x [dir] different from -X dir ?
  • What does -I dir (I know that it adds dir as a directory for loading libraries)?
+4
source share
3 answers

Create a test.rb file in the home directory with the following parameters:

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

Now, if we try to run it:

 ruby -C /home/my_home test.rb 

Which means changing the working directory in / home / my _home and running test.rb, you get an error message:

 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 print "here" and we will not get an error. The difference between main between -x and -C is that -x removes everything up to the line #!/usr/bin/ruby . And you also don't need to install the directory on cd when using -x. Since the main purpose of -x is to delete lines, and if necessary, it also includes -C functionality.

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

See (ruby --help)

  • -Cdirectory cd to the directory before executing the script
  • -x [directory] disable text to line #! ruby and possibly cd to directory

As for -I. You can provide directories in which ruby ​​will look for the file that you are executing or requiring.

 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 the directory before execution, but will just look for files there.

+4
source
Options

-C and -X perform the same task (before executing the "Change directory" command). There is no difference.

-I is used to add the path to $ LOAD_PATH

For example: suppose you have a ruby ​​file my_print_class.rb in the my_lib directory my_print_class.rb: (~ / my_lib / my_print_class.rb)

 class MyPrintClass def self.my_print(str) puts str end end 

Now you have my_call.rb in the house (~).

~ / my_call.rb:

  require 'my_print_class' MyPrintClass.my_print("Hello world") 

For this you need the path my_print_class, so you use ruby ​​-I my_lib my_call.rb

http://www.tutorialspoint.com/ruby/ruby_command_line_options.htm

+2
source

As you can see from man ruby or some documents on the Internet, -C and -X same.

And -I will add some directory to ruby ​​LOAD_PATH. For example, I have ./a/my.rb and `./test.rb 'as follows:

 # ./a/my.rb def hello puts 'hello from a/my' end # ./test.rb require 'my' hello 

And I do ruby -I ./a test.rb This will print hello from a/my . Without -I ruby will report an error: cannot load such file -- my because ./a not in the current LOAD_PATH.

+2
source

All Articles