|
Open the run option and write cmd to open the editor, write ruby -v and press enter to get the details of the version
If we are using a word-processing program as our editor, we need to save the file as a plain text. Then write the code as shown below with a sample program example into a text file, and save it under the filename Sample1.rb in our ruby4rails directory.
#this is a sample program
a = "This is"
b = "my first Ruby Program"
print "The concatenated line is : "
print a << b
Save the above written code as "Sample1.rb".
[edit] Checking the program code for errors and syntax in Ruby
We now have the code written for syntax checking and execution to get the desired result as the output of the code.
We will now check the syntax of the code and then the errors if any in the code.
To run a syntax check on our file, we need to write the below written statement:
$ ruby -cw sample1.rb
Here -c flag means to check the file for any syntax error and -w flag turns on the indication for the warning.
Now considering that we the interpreter didn’t notice any syntax error in our code we get the message as :
Syntax OK
on our screen, which states that the program is ready for the execution.
Here, the Ruby interpreter can check programs for syntax errors without running the programs. It scans the entire file by going through it and intimates the programmer if there is any syntax error which needs attention.
[edit] How to Execute and Run programs in Ruby
To run and execute this program we can simply do that by typing the filename and no extensions.
Now run the program by writing:
$ ruby sample1.rb
And we get the result as below on our screen as the concatenated line as :
The concatenated line is: This is my first Ruby Program.
|