Ruby/Writing Programs in Ruby

From Meshplex

Jump to: navigation, search
Image:Ruby_on_rails_tutorials.jpg
Ruby for complete beginners
Ruby Introduction
What can I use RoR for?
Reasons for choosing RoR over other popular programming languages such as php or asp.net .What makes Ruby so much more special
Where can I find RoR? In what “forms” does it come?
How to install RoR.Solutions for both the novice and professional programmers on Windows,Mac OS X and Linux.Prerequisites.
Ruby programming tutorials for beginners:
Ruby Basics
** Writing Programs in Ruby
** Ruby and the Text Editors
** Escape Character Sequence
** Ruby Arithmetic
** Comments in Ruby
** Ruby Documentation
Ruby Variables, Datatypes, Operators
Ruby Symbols
Ruby Statements
Ruby Converting data to another type: type conversion or typecasting
Ruby Arrays, Hashes, Ranges
Ruby Functions and built in functions
Ruby Control structures
Ruby Regular expressions and blocks
Ruby Loops
Ruby Recursion
Ruby Data Structures
Ruby Methods, Classes, Modules, Namespaces
Ruby Exceptions
Ruby Object Oriented Programming
Ruby Multithreading
Ruby File Handling.Input and Output
Ruby Basic GUI
Ruby and databases.Ruby on Rails and MySQL
Ruby Basic CGI.Using fastCGI
Ruby Basic Networking and web programming
Ruby Basic Graphics
Ajax and Rails.Web 2.0 and what it means
Ruby Testing, Debugging, Automation of tasks
Ruby Apache,Capistrano, Mongrel,lighttpd – reviews and tips
Finding a Ruby on Rails ready web hosting company
BONUS: mini tutorial for a simple RoR application


[edit] How to Open, Save, Close,Edit and Run programs in Ruby

We have the choice of using any text editor as discussed in the editor section of this tutorial.

Ruby provides the flexibility on the basis of the operating systems that we can execute and run a ruby program by just giving the filename and this doesn’t need to mention any extension.

We may need to put .rb filename extension in few programs to get them executed successfully.

We need to pass the file for processing and execution to ruby interpreter twice. Once to go for the proper syntax errors and upon receiving the success status second time to execute it. We need to give –c and –w as command line switches to get the checking status for errors and warnings if required Here –c states the checking for syntax errors and –w demonstrates the warnings messages to be sent to programmer .If the execution is done successfully we get the actual result displayed on our computer screens.

[edit] How to get started and Open programs in Ruby

To write our first Ruby program we need to open either a terminal say Unix or can operate on command line in Windows. This can be done by selecting the "Run" option from start menu and then write cmd on it which stands for cmd. This will display the editor on which we can write the code of our first program.

We will get the information like this on our screen :

 Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
C:\Documents and Settings\Tim>_

[edit] Writing and Saving the First Ruby Program

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".

Checking the program code for errors and syntax in Ruby<h3>

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.

<h3>How to Execute and Run programs in Ruby<h3>

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.
Next
Personal tools