Thursday, September 3, 2020
How to Use the Command Line to Run Ruby Scripts
The most effective method to Use the Command Line to Run Ruby Scripts Prior to truly beginning to utilize Ruby, you have to have an essential comprehension of the order line. Since most Ruby contents wont have graphical UIs, youll be running them from the order line. In this way, youll need to know, at any rate, how to explore the index structure and how to utilize pipe characters, (for example, |, à and ) to divert info and yield. The orders in this instructional exercise are the equivalent on Windows, Linux, and OS X. To begin an order brief on Windows, go to Start - Run. In the exchange that shows up, enter cmd into the info box and press OK.To start an order brief on Ubuntu Linux, go to Applications - Accessories - Terminal.To start an order brief on OS X, go to Applications - Utilities - Terminal. Once youre at the order line, youll be given a brief. Its frequently a solitary character, for example, $ or #. The brief may likewise contain more data, for example, your username or your present registry. To enter a commandâ all you have to do is type in the order and hit the enter key. The primary order to learn is the compact disc order, which will be utilized to get to the index where you keep your Ruby records. The order underneath will change catalog to the scripts registry. Note that on Windows frameworks, the oblique punctuation line character is utilized to delimit registries however on Linux and OS X, the forward slice character is utilized. C: ubycd scripts Running Ruby Scripts Since you realize how to explore to your Ruby contents (or your rb records), its chance to run them. Open your content manager and spare the accompanying system asâ test.rb. #!/usr/canister/env ruby à print What is your name? name gets.chomp puts Hello #{name}! Open an order line window and explore to your Ruby contents index utilizing theâ cdâ command. Once there, you can list records, utilizing theâ dirâ command on Windows or theâ lsâ command on Linux or OS X. Your Ruby records will all have the .rb document expansion. To run the test.rb Ruby content, run the commandâ ruby test.rb. The content ought to approach you for your name and welcome you. On the other hand, you can design your content to run without utilizing the Ruby order. On Windows, theâ one-click installerâ already set up a record relationship with the .rb document expansion. Basically running the commandâ test.rbâ will run the content. In Linux and OS X, for contents to run consequently, two things must be set up: a kit n kaboodle line and the document being set apart as executable. The thing line is as of now accomplished for you; its the primary line in the content beginning withâ #!. This mentions to the shell what sort of document this is. For this situation, its a Ruby document to be executed with the Ruby translator. To check the document as executable, run the commandâ chmod x test.rb. This will set a record consent bit demonstrating that the document is a program and that it very well may be run. Presently, to run the program, just enter the commandâ ./test.rb. Regardless of whether you summon the Ruby translator physically with the Ruby order or run the Ruby content legitimately is up to you. Practically, they are something very similar. Use whichever strategy you feel generally good with. Utilizing Pipe Characters Utilizing the funnel characters is a significant ability to ace, as these characters will change the info or yield of a Ruby content. In this model, theâ â character is utilized to divert the yield of test.rb toâ a text fileâ called test.txt as opposed to printing to the screen. On the off chance that you open new test.txt document after you run the content, youll see the yield of the test.rb Ruby content. Realizing how to spare yield to a .txt record can be helpful. It permits you to spare program yield for cautious assessment or to be utilized as contribution to another content sometime in the future. C:scriptsruby example.rb test.txt So also, by utilizing theâ â character rather than theâ â character you can divert any info a Ruby content may peruse from the console to peruse from a .txt record. Its accommodating to think about these two characters as channels; youre piping yield to records and contribution from documents. C:scriptsruby example.rb At that point theres the funnel character,â |. This character will pipe the yield from one content to the contribution of another content. Its what could be compared to channeling the yield of a content to a record, at that point piping the contribution of a second content from that document. It just abbreviates the procedure. Theâ |â character is valuable in making channel type programs, where one content creates unformatted yield and another content arrangements the yield to the ideal configuration. At that point the subsequent content could be changed or supplanted totally without adjusting the main content by any means. C:scriptsruby example1.rb | ruby example2.rb The Interactive Ruby Prompt An incredible aspect concerning Ruby is that its test-driven. The intuitive Ruby brief gives an interface to the Ruby language for moment experimentation. This proves to be useful while learning Ruby and exploring different avenues regarding things like standard articulations. Ruby explanations can be run and the yield and return esteems can be inspected right away. On the off chance that you commit an error, you can return and alter your past Ruby explanations to address those mix-ups. To begin the IRB speedy, open your order line and run theâ irbâ command. Youll be given the accompanying brief: irb(main):001:0 Type theâ hello worldâ statement weve been utilizing into the brief and hit Enter. Youll see any yield the announcement produced just as the arrival estimation of the announcement before being come back to the brief. For this situation, the announcement yield Hello world! furthermore, it returnedâ nil. irb(main):001:0 puts Hello world! Hi world! nilf irb(main):002:0 To run this order once more, basically press the up key on your console to get to the announcement you recently ran and press the Enter key. In the event that you need to alter the announcement before running it once more, press the left and right bolt keys to move the cursor to the right spot in the announcement. Make your alters and press Enter to run the new order. Squeezing up or down extra occasions will permit you to look at a greater amount of explanations youve run. The intelligent Ruby device ought to be utilized all through learning Ruby. At the point when you find out about another element or simply need to take a stab at something, fire up the intelligent Ruby brief and attempt it. See what theâ statementâ returns, passâ different parametersâ to it and simply do some broad testing. Having a go at something yourself and seeing what it jars be significantly more important than simply finding out about it!
Subscribe to:
Posts (Atom)