Changes

Simple Ruby Examples

14 bytes added, 13:48, 31 March 2009
no edit summary
</pre>
As you can see, all we need to do to output some text is enter a single line of Ruby code. You may recall, if you have read the previous chapters, that one of the strengths of Ruby is that it is fast and intuitive to learn. Clearly it would be hard to make printing a line of text any easier than a ''print'' statement followed by the text to be displayed (together with a newline character '\n' on Linux and UNIX).
As an example, let's compare this to the equivalent code in Java:
== Interactive Ruby Execution ==
<google>ADSDAQBOX_FLOW</google>
In the [[What is Ruby?]] we discussed the fact that Ruby is an interpreted language. This essentially means that Ruby source code is compiled and executed at run time, rather than pre-compiled as is the case with languages such as C or C++. One of the advantages of being an interpreted language is that we can write Ruby code directly into the interpreter and have it executed interactively and in real-time. This is a great way to learn Ruby and to try out different code structures.
Interactive Ruby code is entered using the ''irb'' tool. If you are running Windows and installed Ruby using the one click installer, you already have ''irb'' installed. If you are running on Linux, there is a good chance ''irb'' is not yet installed. Verify the installation as follows:
== Executing Ruby from a File ==
Clearly the command line approach to execution is of limited use once you get beyond a few lines of Ruby script. A much more common approach is to place the Ruby script in a file, and then pass that file to the Ruby interpreter to run. To try this, create a file called ''rubyhello.rb'' using your favorite editor and enter the following lines into it:
<pre>
Placing Ruby code into a file is obviously much easier and practical than using multiple -e command line options. Suppose, however, that we want to go one step further and be able to execute a Ruby based program simply by typing the name of the file containing the code, rather than prefixing it with the ''ruby'' command.
This can be achieved on Linux or UNIX by placing a special line at the top of the script file informing the environment responsible for executing the program (such as a Linux command shell) where to look for the Ruby interpreter. This special line consists of a '#', a '!' and the path the to the ''ruby'' executable and is known affectionately as the ''shebang''.
Firstly, you need to know where ''ruby'' is located on your system. Assuming it is already in your PATH environment variable you can use the ''which'' command to find it:
If you have used Windows extensively you will be familiar with the concept that it is possible to double click, for example, on a .doc file and have that file automatically loaded into Microsoft Word. This works because the system has been configured to associate .doc files with Word. Associating .rb files with Ruby is essentially the same thing.
First, it is important to note that if you installed Ruby on Windows using the One-Click Installer then .rb files will already have been associated with Ruby, so there is no need to perform the steps in this section. Simpy Simply type ''hello.rb'' at the command prompt, and our example will run.
If you built Ruby yourself from the source code, or installed using a mechanism other than the One-Click Installer, you will need to associate .rb files with Ruby. The steps to achieve this are as follows:
</pre>
The above steps can be placed in your Autoexec.bat file if you would like this associated association made every time you reboot your system.
<hr>