Friday, May 15, 2020
How to Use the Command Line to Run Ruby Scripts
Before really starting to use Ruby, you need to have a basic understanding of the command line. Since most Ruby scripts wont have graphical user interfaces, youll be running them from the command line. Thus, youll need to know, at the very least, how to navigate the directory structure and how to use pipe characters (such as |, à and ) to redirect input and output. The commands in this tutorial are the same on Windows, Linux, and OS X. To start a command prompt on Windows, go to Start - Run. In the dialog that appears, enter cmd into the input box and press OK.To start a command prompt on Ubuntu Linux, go to Applications - Accessories - Terminal.To start a command prompt on OS X, go to Applications - Utilities - Terminal. Once youre at the command line, youll be presented with a prompt. Its often a single character such as $ or #. The prompt may also contain more information, such as your username or your current directory. To enter a commandà all you need to do is type in the command and hit the enter key. The first command to learn is the cd command, which will be used to get to the directory where you keep your Ruby files. The command below will change directory to the \scripts directory. Note that on Windows systems, the backslash character is used to delimit directories but on Linux and OS X, the forward slash character is used. Running Ruby Scripts Now that you know how to navigate to your Ruby scripts (or your rb files), its time to run them. Open your text editor and save the following program asà test.rb. #!/usr/bin/env ruby à print What is your name? name gets.chomp puts Hello #{name}! Open a command line window and navigate to your Ruby scripts directory using theà cdà command. Once there, you can list files, using theà dirà command on Windows or theà lsà command on Linux or OS X. Your Ruby files will all have the .rb file extension. To run the test.rb Ruby script, run the commandà ruby test.rb. The script should ask you for your name and greet you. Alternatively, you can configure your script to run without using the Ruby command. On Windows, theà one-click installerà already set up a file association with the .rb file extension. Simply running the commandà test.rbà will run the script. In Linux and OS X, for scripts to run automatically, two things must be in place: a shebang line and the file being marked as executable. The shebang line is already done for you; its the first line in the script starting withà #!. This tells the shell what type of file this is. In this case, its a Ruby file to be executed with the Ruby interpreter. To mark the file as executable, run the commandà chmod x test.rb. This will set a file permission bit indicating that the file is a program and that it can be run. Now, to run the program, simply enter the commandà ./test.rb. Whether you invoke the Ruby interpreter manually with the Ruby command or run the Ruby script directly is up to you. Functionally, they are the same thing. Use whichever method you feel most comfortable with. Using Pipe Characters Using the pipe characters is an important skill to master, as these characters will alter the input or output of a Ruby script. In this example, theà à character is used to redirect the output of test.rb toà a text fileà called test.txt instead of printing to the screen. If you open new test.txt file after you run the script, youll see the output of the test.rb Ruby script. Knowing how to save output to a .txt file can be very useful. It allows you to save program output for careful examination or to be used as input to another script at a later time. C:\scriptsruby example.rb test.txt Similarly, by using theà à character instead of theà à character you can redirect any input a Ruby script may read from the keyboard to read from a .txt file. Its helpful to think of these two characters as funnels; youre funneling output to files and input from files. C:\scriptsruby example.rb Then theres the pipe character,à |. This character will funnel the output from one script to the input of another script. Its the equivalent of funneling the output of a script to a file, then funneling the input of a second script from that file. It just shortens the process. Theà |à character is useful in creating filter type programs, where one script generates unformatted output and another script formats the output to the desired format. Then the second script could be changed or replaced entirely without having to modify the first script at all. C:\scriptsruby example1.rb | ruby example2.rb The Interactive Ruby Prompt One of the great things about Ruby is that its test-driven. The interactive Ruby prompt provides an interface to the Ruby language for instant experimentation. This comes in handy while learning Ruby and experimenting with things like regular expressions. Ruby statements can be run and the output and return values can be examined immediately. If you make a mistake, you can go back and edit your previous Ruby statements to correct those mistakes. To start the IRB prompt, open your command-line and run theà irbà command. Youll be presented with the following prompt: irb(main):001:0 Type theà hello worldà statement weve been using into the prompt and hit Enter. Youll see any output the statement generated as well as the return value of the statement before being returned to the prompt. In this case, the statement output Hello world! and it returnedà nil. irb(main):001:0 puts Hello world! Hello world! nilf irb(main):002:0 To run this command again, simply press the up key on your keyboard to get to the statement you previously ran and press the Enter key. If you want to edit the statement before running it again, press the left and right arrow keys to move the cursor to the correct place in the statement. Make your edits and press Enter to run the new command. Pressing up or down additional times will allow you to examine more of statements youve run. The interactive Ruby tool should be used throughout learning Ruby. When you learn about a new feature or just want to try something, start up the interactive Ruby prompt and try it. See what theà statementà returns, passà different parametersà to it and just do some general experimenting. Trying something yourself and seeing what it does can be a lot more valuable than just reading about it!
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.