Ruby Tutorial

30 Jul, 2021
Ruby is a object-oriented, reflective, general-purpose, dynamic programming language. Ruby was developed to make it act as a sensible buffer between human programmers and the underlying computing machinery. It is an interpreted scripting language which means most of its implementations execute instructions directly and freely, without previously compiling a program into machine-language instructions. Ruby is used to create web applications of different sorts. It is one of the hot technology at present to create web applications. 
Topics:  
 
  
Ruby has many reasons for being popular and in demand. Few of the reasons are mentioned below: 
The code written in Ruby is small, elegant and powerful as it has fewer number of lines of code.Ruby allows simple and fast creation of Web application which results in less hard work.As Ruby is free of charge that is Ruby is free to copy, use, modify, it allow programmers to make necessary changes as and when required.Ruby is a dynamic programming language due to which there is no tough rules on how to built in features and it is very close to spoken languages.
The code written in Ruby is small, elegant and powerful as it has fewer number of lines of code.
Ruby allows simple and fast creation of Web application which results in less hard work.
As Ruby is free of charge that is Ruby is free to copy, use, modify, it allow programmers to make necessary changes as and when required.
Ruby is a dynamic programming language due to which there is no tough rules on how to built in features and it is very close to spoken languages.
 
 
 
Ruby is an interpreted, high-level, general-purpose programming language. Ruby is dynamically typed and uses garbage collection. It supports multiple programming paradigms, object-oriented, including procedural and functional programming. Ruby is based on many other languages like Perl, Lisp, Smalltalk, Eiffel and Ada. This language has an elegant syntax that is natural to read and easy to write. Popular Ruby Editors/IDE are below.  
Notepad/gedit : They are simple text-editor for writing ruby programs. Notepad is available on Windows and gedit is available on Linux.
NetBeans : It is a well known free IDE(Integrated Development Environment) for developing softwares in ruby. we can download NetBeans from here.
  
 
We can download Ruby from rubyInstaller.org Click on any link as your preferred version depending on our Windows for say we can go with WITHOUT DEVKIT versions like Ruby 2.6.4-1 (x64) for Windows(64 bit) and the 2nd link which says Ruby 2.6.4-1 (x86) is for Windows(32 bit) as highlighted below, which is the latest version.After downloading the file, run the .exe file and follow the instructions to install Ruby on your Windows. Once you installed Ruby with default settings on your Windows, you have to setup environment variable.Go to Control Panel -> System and Security -> System. Then click on Advanced System Setting option then under Advanced tab click on Environment Variables.Now, we have to Edit the “Path” variable under System variables so that it also contains the path to the Ruby environment. Under the System Variable select the “Path” variable and click on Edit button.We will see list of different paths, click on New button and then add path where Ruby is installed. By default, Ruby is installed in “C:\Ruby26-x64\bin” (in our case) folder OR “C:\Ruby26-x86\bin”. In case, you have installed Ruby at any other location, then add that in path, for example: if you have installed Ruby on other drive then go to that driver and locate the Ruby folder then inside the ruby folder a folder called bin will be there so copy the path and include it to System Variable’s Path like SomeDrive:\SomeFolder\RubyXX-xYY\binClick on OK, Save the settings and we are done. Now to check whether installation is done correctly, open command prompt and type ruby -v and hit Enter. You will see some output like ruby 2.6.4p104 (2019-08-28 revision 67798) [x64-mingw32](in our case) on the console. It means we have successfully installed Ruby and we are good to go.  
We can download Ruby from rubyInstaller.org Click on any link as your preferred version depending on our Windows for say we can go with WITHOUT DEVKIT versions like Ruby 2.6.4-1 (x64) for Windows(64 bit) and the 2nd link which says Ruby 2.6.4-1 (x86) is for Windows(32 bit) as highlighted below, which is the latest version.
After downloading the file, run the .exe file and follow the instructions to install Ruby on your Windows. Once you installed Ruby with default settings on your Windows, you have to setup environment variable.
Go to Control Panel -> System and Security -> System. Then click on Advanced System Setting option then under Advanced tab click on Environment Variables.
Now, we have to Edit the “Path” variable under System variables so that it also contains the path to the Ruby environment. Under the System Variable select the “Path” variable and click on Edit button.
We will see list of different paths, click on New button and then add path where Ruby is installed. By default, Ruby is installed in “C:\Ruby26-x64\bin” (in our case) folder OR “C:\Ruby26-x86\bin”. In case, you have installed Ruby at any other location, then add that in path, for example: if you have installed Ruby on other drive then go to that driver and locate the Ruby folder then inside the ruby folder a folder called bin will be there so copy the path and include it to System Variable’s Path like SomeDrive:\SomeFolder\RubyXX-xYY\bin
Click on OK, Save the settings and we are done. Now to check whether installation is done correctly, open command prompt and type ruby -v and hit Enter. You will see some output like ruby 2.6.4p104 (2019-08-28 revision 67798) [x64-mingw32](in our case) on the console. It means we have successfully installed Ruby and we are good to go.  
 
 
Go to Application -> TerminalType command as below.sudo apt install ruby-fulland press enter and enter your password. Wait for it to complete the download and then it will install Ruby on your machine.We are done installing Ruby on Linux. Now to check whether installation is done correctly, type ruby -v in the Terminal.If you see some text like ruby 2.6... it means u have successfully installed Ruby on your Linux.
Go to Application -> Terminal
Type command as below.sudo apt install ruby-fulland press enter and enter your password. Wait for it to complete the download and then it will install Ruby on your machine.
sudo apt install ruby-full
and press enter and enter your password. Wait for it to complete the download and then it will install Ruby on your machine.
We are done installing Ruby on Linux. Now to check whether installation is done correctly, type ruby -v in the Terminal.If you see some text like ruby 2.6... it means u have successfully installed Ruby on your Linux.
 
 
With Online IDE : Let’s consider a simple Hello World Program. 
Ruby
puts "Hello World"
Output :  
Hello World
Above code will run on online IDE. Here, puts keyword is used to print any thing on the screen.
With Linux : Using Command-Line Firstly, open a text editor Notepad or Notepad++. write the code in the text editor and save the file with (.rb) extension. open the command prompt follow step by step process on our system.
To run the hello.rb Ruby script, run the command ruby hello.rb it will print the output.
With Window : Using Command-Line write ruby -v on command line window to show ruby version. Below is the image to better understand.
To start the IRB prompt, open your command-line and run the irb command. after this we can write the ruby code and it will run on command line.
In above image we use puts keyword to print the output and it returned nil.Local variablesInstance variablesClass variablesGlobal variables
Local variables
Instance variables
Class variables
Global variables
if statement
if-else statement
if-else-if ladder statement
Ternary statement
for loop Example :  
Ruby
# Ruby program to illustrate 'for'  # loop using range as expression     i = "Sudo Placements"    # using for loop with the range for a in 1..5 do         puts i      end
Sudo Placements
Sudo Placements
Sudo Placements
Sudo Placements
Sudo Placements
 
while loop Example :  
Ruby
# Ruby program to illustrate 'while' loop     # variable x x = 4    # using while loop  # here conditional is x i.e. 4 while x >= 1     # statements to be executed   puts "GeeksforGeeks"  x = x - 1      # while loop ends here end
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
 
do..while loop Example :  
Ruby
# Ruby program to illustrate 'do..while'loop     # starting of do..while loop loop do         puts "GeeksforGeeks"      val = '7'      # using boolean expressions  if val == '7'  break end     # ending of ruby do..while loop  end 
GeeksforGeeks
 
until loop Example :  
Ruby
# Ruby program to illustrate 'until' loop     var = 7    # using until loop # here do is optional until var == 11 do      # code to be executed   puts var * 10  var = var + 1      # here loop ends end
Output :  
70
80
90
100
Polymorphism
Inheritance
Encapsulation
Class & Object
Constructors
\w is equivalent to [0-9a-zA-Z_]
\d is the same as [0-9]
\s matches white space
\W anything that’s not in [0-9a-zA-Z_]
\D anything that’s not a number
\S anything that’s not a space
The dot character . matches all but does not match new line. If you want to search . character, then you have to escape it.>/li>
Akanksha_Rai
sagar0719kumar
Ruby
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Make a Custom Array of Hashes in Ruby?
Include v/s Extend in Ruby
Global Variable in Ruby
Ruby | Enumerator each_with_index function
Ruby | Array select() function
Ruby | Case Statement
Ruby | unless Statement and unless Modifier
Ruby | Hash delete() function
Ruby | Data Types
Ruby | Array class find_index() operation