简体   繁体   中英

Ruby - How to use -r switch with ruby command line tool

I was trying to figure out how to work the command line switch -r.

My understanding is that the code is typed out as follows:

ruby -r*nameOfRequired*

I am finding that this is not the case. When I type out the above and press enter, the terminal expects an "end of input syntax" and does not continue.

What am I missing? Does there need to be a space in between the switch and the name of the required file?

Please and thank you!


EDIT: I am currently reading "The Well Grounded Rubyist" by David A. Black, and I came up with this question while reading the section on command line switches.

Having said that, I created a "test.rb" file, containing:

puts Date.today

Then, in the terminal, I typed out:

ruby -r date

I thought this would 'require' the date module, and then enable me to run the "test.rb" file, using ruby test.rb (given that I am in the correct directory).

Instead, the terminal cursor moves to a newline, expecting more input. Let me know if I need to clarify anything else. Thanks!

If you just type ruby -r module , then Ruby will load the module and wait for you to type the main program that requires that module.

If you just want to run the module and do nothing else, you can do do ruby full-path-to-module without the -r , or ruby -r module -e exit , or ruby -r module </dev/null , or similar.

According to ruby -h :

-rlibrary require the library, before executing your script

Without giving your script file path, it read the script from stdin.

Try following (You can omit script file path when you give -e command ):

ruby -r**nameOfRequired** -e ""

In general, the ruby command does not record any state from one run to the next, so you need to tell it every thing that it needs to know whenever you run it.

Whenever you run it, you need to tell it the main program to run or else it will expect you to type that program on the standard input. The -r does not specify the main program.

Try this:

ruby -rdate test.rb

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM