An Introduction to Ruby
Ruby
Ruby is essentially a scripting language. Talking about some of its important features, it's thoroughly object-oriented, dynamically typed and an imperative programming language. It is, in many ways, similar to Python, but differs from it in case of execution and syntax (Introduction to Python here. What sets Ruby apart from the pack, is the fact that it is very easy for beginners to grasp, and at the same time powerful enough for the most advanced programmers.One of the best uses of Ruby is as a prototyping language, you can have fully-functional working model of your application, in the same time you may have spent floundering to set up the framework while using other programming languages. But its application is not limited to prototyping. Ruby is used extensively almost across the entire spectrum – a programming language can have simulations, 3D modelling, business, robotics, telecommunication, web applications etc.
Installation
We'll discuss the current stable iteration of the language i.e. Ruby 1.9.2. There are three options you can follow to install Ruby:- Compile from the source code
- Use RVM (Ruby Version Manager)
- Use Ruby Installer
NOTE: If you are running Linux, your distribution repository may also have Ruby e.g. For Ubuntu use the command sudo apt-get install ruby1.9.1 (This will install 1.9.2, the name is kept for library compatibility) On other systems it is prudent to use RVM. Mac OS X has a built-in support for Ruby (one of the reasons, for Ruby becoming famous) Lion supports 1.8.7, again here RVM is advised as Mac OS X is a Unix based system.
If you are running Windows, neither of the two methods mentioned above will work for you. Download the Ruby installer from its website. Run the setup and you will be greeted with the usual fare; license and so on. The important part in the setup is selecting which features you want, when you reach the screen, same as the one shown below, select options as shown (We've left out the installation of Tcl/Tl toolkit as we will not be working on GUI-based programs, you can install it you do plan to work on the same)

Select the location for your Ruby installation

This shouldn't take too long!

And you are done, you have successfully installed Ruby on your system, now on to coding.
The customary greeting
It is an unwritten rule in the world of programming that, you cannot start learning any programming language without greeting the world, first-hand. So we'll start with simple Hello World! Program. Fire up the terminal or the command prompt, type in irb (which stands for Interactive Ruby, and is a fantastic way to learn and test code in Ruby) and hit enter, you will be greeted with the irb prompt, enter the code snippet shown below puts “Hello World!!”
It will look something like this. And Voila! You are done.
Yes, it may seem unbelievable but you just ran your first Ruby code.
If this is not tough or challenging enough for you, we’ll execute the
same program in a different way. Open Notepad or any text editor of your
choice, enter the same code snippet and Save the File as hello.rb Now
fire up the command prompt, navigate to the folder where your source
code recides and enter the command below

And once again you are done.
Perhaps you would want your program to do something more, say greet the
world multiple time, here is how you go about it.
\5.times {
puts "Hello World!!"
}
#include
void main() {
int i = 5;
while (i != 0) {
printf("Hello World!!n");
i--;
}
}
NOTE: The example discussed, justifies the statement that Ruby is thoroughly or we can say strictly object-oriented. Even a primitive data type such as Integer is encapsulated in an object. (For those not familiar with object-oriented programming an introduction to OOP here)
Executing your Ruby Scripts
Moving further, those of you who have a little background in programming will be able to recognize this pattern
*
**
***
****
*****
Now how would you have gone about writing a program which generates the aforementioned pattern in C? The code would most probably look something like this:
#include
void main() {
int i,j;
for(i = 1; i <= 5; i++) {
for(j = 1; j <= i ; j++) {
printf("*");
}
printf("n");
}
}
i = 1
(
j = 0
(
print "*"
j += 1
) until j == i
print "n"
i += 1
) until i == 6
Expression until Condition
Also the variables are not declared, you can just assign value to variable to use it. The variables also do not need to be type-casted, Ruby handles that for us. And we’ve used print here instead of puts, as puts adds a new-line character at the end of the string, which will result in a line rather than a triangle of made up of *s. What changes, do you think, will have to be made to get a pattern like this:
1
12
123
1234
12345
Keep in mind, that the code mentioned here can be directly executed using irb or you can go for the other method in which you make .rb file and run it using the Ruby interpreter (i.e. the ruby command on the command-line or terminal).
if you have any problem then please refer to google as i am a beginner on ROR!!!!!!!! :-)
ReplyDelete