Learn to program with Ruby #Issue1

in #utopian-io7 years ago (edited)

Learning to program can be done with any general language: the same rules basic are used regardless of the language. However, some are closer than others to the human logic and therefore better adapted to the learning of programming. This is the case of Ruby.
ruby.jpg
Image Source

The methodology of programming is simple:

  • 1 . We write the program in the chosen language. We do that by means of a text editor, a software of treatment of text adapted to the writing of computer programs. The result is the source code. It's ordinary text, but with a very precise and language-specific syntax.
  • 2 . We test the program, we correct the errors if there are any and weretest. This continues until there is no more error.

Afterwards, when we want to run the program, we go intothe command interface and you type (in the case of Ruby):
ruby file-name.rb
The extension rb means that it is source code in Ruby.
The ruby ​​command tells the computer that it should launch the Ruby interpreter. An interpreter is a translator, a program responsible for transforming the text of the source code into a sequel instructions directly understandable by the computer.
The fact that Ruby is an interpreted language means that you can not run a Ruby program if the language has not been previously installed on the machine.


Ruby Installation


Mac OS: Ruby is preinstalled and ready to use.
Windows : By default, Ruby is not installed. Go to http://www.ruby-lang.org/en/downloads, click on Download Ruby, then Ruby xxx One-Click Install and track instructions.
Linux: Ruby is often preinstalled, but not always. There are many ways to find out, but the best is to use the which command, which has the advantage of being present on all operating systems of the Unix family, and which serves to indicate where is a program.
Type: which ruby
For example, if the system answers / usr / bin / ruby, Ruby is installed in the specified location. if does not answer anything or if it displays a negative message (like no ruby ​​in ...), you have to install the language.

For this, we go through the system's package manager. If it's apt-get, type:
sudo apt-get install ruby ​​irb rdoc
Once the installation is complete, we can check that Ruby is installed and know by the same occasion of which version of the language it is concerned by typing:
ruby -v


The command interface


The manipulations described in this text are done in the system control interface
operating.

On Windows, go to Start → All Programs → Accessories → Prompt command or by Start → Run by typing cmd.exe. Attention, by default, the option Run no longer exists from Vista. To activate it, right click on the icon of the Start menu, Properties → Start Menu → Customize and check the Run command line.

On Mac OS, it's Finder → Applications → Utilities → Terminal.app.
On Linux, you can either type Alt-Ctrl-F1 (to return to the graphical interface, it's Alt-Ctrl-F7), either go through the menus: Applications → Accessories → Terminal, or click on the icon is a black screen on the taskbar, if this icon exists, right click again on the bottom of the screen and select Open Terminal from the menu that appears.


First step


Tradition has it that the first example given of a program is the one that displays the Hello World words on the screen.
To do this, open a text editor and type the source code.
Here, it's just puts "Hello World!" (The word puts is an abbreviation for put string, "show" string of characters ") :
puts "Hello World !"
We finish by registering it under any name, for example test1.rb or helloworld.rb.
The extension is rb.

Concretely, here is what it gives with the Notepad of Windows or with nano of Unix:

Notepad in WindowsWith nano

rdt1.png

We run the program by going into the command interface and typing:
ruby helloworld.rb
The system must respond by displaying Hello World! If it is not the case, it is because we have made a error. A common problem is to forget a space or put one where it should not.


C ++, C # and Java


At this point, it can be interesting to see the same program in the three most currently used: C ++, C # and Java.1 They all come from C language.
In C ++:

#include <iostream>
int main ()
{
 std :: cout << "Hello World! \ n";
 return 0;
}

In Java:

HelloWorld class
{
 public static void main (String [] args)
 {
 System.out.println ("Hello World!");
 }
}

In fact, comparing the number of C #, C ++, and Java users does not make much sense. It's a as if we say that English and Chinese are the first two languages ​​in the world. In reality, the Chinese is well spoken by a lot of people, but they are almost all in China, so that English is spoken all over the world. In the same way, the C # is practically used only in the Windows world while C ++ and Java are universal.
And in C #:

using System;
public class HelloWorld
{
 public static void Main()
 {
 System.Console.WriteLine("Hello World !");
 }
}

We see that these languages ​​are heavier than Ruby, but that does not mean they are not as good. It's a different philosophy.
Note the indentation, the fact that the lines are shifted to make the code more readable. By example, in the C # code above, we see that the public class instruction HelloWorld concerns the code that lies between the braces of the third and the last line and that the instruction public static void Main () is located inside.
By the way, it should be noted that other languages ​​than Ruby are light. This is the case of Python. here is the Hello World program in this language:
print "Hello World !"

This program is run using the helloworld.py python command, as long as the language is present on the system; this is the case with Mac OS and often Linux, but not with Windows. It can be downloaded from the official website (http://www.python.org/download).


Interactive Ruby


Ruby includes an interactive version called irb (Interactive Ruby). We throw it by typing irb in the control interface. Here's what it gives on Mac OS (left) and Ubuntu Linux (on right):
rbt2.png
And under Windows:
rbt3.png

On Linux and Windows, you can type irb --simple-prompt instead of irb if you prefer the minimal ">>" prompt as on Mac OS.
For the Hello World program to run in irb, you type:-
puts "Hello World !"

he interpreter responds by displaying the requested message followed by "=> nil ", which means that there is no value to return. On the other hand, if we type 2 + 2, the system returns the value 4 (see the image opposite). Irb is very convenient as a sandbox. Do not hesitate to do many tests in this environment, this is how we learns to program.
rbt4.png
We come out by typing exit.

Ruby is an object language. What does that mean ? In practice, it's simple: in Ruby, all you can manipulate is an object, and you can exercise on any object actions predefined called methods. In Ruby, when we calculate 6 + 3 = 9, we apply the action add to objects 6 and 3.

The basic syntax is very simple too: the name of the object is followed by that of the method with a point of separation between the two.
NameObjet.NameMéthode
Since in Ruby everything is an object, strings are objects. What can we do with them ? For example, we can put the first letter in capitalize, put the whole string in uppercase (upcase) or measure its length (size or length, these are synonyms). We can reverse the characters of the string and transform the Zorglub object into bulgroZ as a tribute to Franquin.

We can also apply a succession of several methods to an object. So, the line "Zorglub" .upcase.reverse applies the action to reverse the letters on the object ZORGLUB intermediate obtained by the action capitalize on the object original Zorglub.

Of course, numbers are also objects, and we can also apply actions on them. For example, we can round a number (round), ask for the following integer (succ or next) or take its value absolute (abs).
rbt5.png

In Ruby, there are two types of numbers: Integers and Floating Numbers (floats), numbers that have decimal digits such as 2.7 or 3.1416. We can change a floating point number (to_f), or the opposite (to_i), or even change it to string (to_s). For example, 6 is an integer, 6.0 is a floating point number and "6" a string of characters.

Attention, if the rounding of 3.9 gives well 4, the transformation of 3.9 in integer gives 3. This also explains that 3/2 gives 1. If we want the good answer, you have to work with floating point numbers and not integers. For that, you have to type 3.0 / 2.0. The result is 1.5.

N.B .: as always in computer science, the French comma is replaced by the Anglo-Saxon point. We do not write 1.5 but 1.5.

We can also act on variables, which are objects whose value can change. For example, we can create a variable called total and it give the initial value 20 then act on this value.
If we add 1 and we ask his successor, the result is the same: 21. This last operation does not give 22 because the action adding 1 to total does not change the value of the total variable. By against, this is the case with the formula total = total + 1 which means to modify the present value of total by adding 1.
rbt6.png

It has been said, a method is an action predefined. A method that is not previously defined is misunderstood and causes the message error undefined method.
However, predefined does not mean stuck. We will see further that we can create our own methods by means of the construction def ... end.

Objects are part of classes. A class is what defines a set of objects, which makes that they resemble each other and that they are different from objects of other classes. By for example, strings form a class called String. A class describes the properties (or attributes) of its objects as well as their behavior as defined by the methods.

There are also subclasses. So, in Ruby, the class Numeric defines what characterizes a number in general, and subclasses develop what characterizes each type of number (integer, floating point number, etc.). A mother class is called a metaclass
.
Subclasses inherit methods from their parent class - it's the notion of inheritance - and add theirs, the ones that their are specific.

In Ruby, there is a parent class of all classes: Object. It is she who defines the essential methods, those which concern all types of objects. Thus, the fact that the methods "==" (equality) and "! =" (In- of this class results in that an object of any type can be compared to another object of any type.

Polymorphism is another important notion. It defines the idea that a given method can be applied to different object classes. In other words, the method does not behave the same way according to the class - it adapts to its customers. For example, with the method Add, 2 + 2 gives 4, but "te" + "st" gives "test".

Tutorial Continued in next post...........

Thanks For Reading



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Your contribution cannot be approved because it does not follow the Utopian Rules.

  • This was previously rejected because of the wrong repo.
  • Sharing again in not acceptable.
    You can contact us on Discord.
    [utopian-moderator]

Coin Marketplace

STEEM 0.18
TRX 0.16
JST 0.029
BTC 76511.74
ETH 3031.28
USDT 1.00
SBD 2.62