Home > Blog > Ruby Tutorial - Guide to Learn Ruby Programming Language

Ruby Tutorial - Guide to Learn Ruby Programming Language

Ruby Tutorial - Guide to Learn Ruby Programming Language

By Upskill Campus
Published Date:   5th February, 2024 Uploaded By:    Priyanka Yadav
Table of Contents [show]

 

If you want to learn Ruby and become a Ruby developer, you're on the right path. So, where should you start? Nevertheless, you should begin with the basics of programming. As a result, you can easily understand each concept regarding the ruby tutorial. 

 

What is Ruby Programming Language?

 

Ruby is a free-to-use programming language that's like a dynamic, open book. Moreover, it is just like other languages, such as Perl and Smalltalk. In addition, it is highly compatible with all Operating Systems, such as Windows, Mac OS, and even UNIX.

Ruby is a language where it's all about objects, like having special tools. Additionally, every piece of code has its gear – we call them properties (like tools you can use) and actions (like cool moves you can do). In Ruby, it's all about making things organized and fun.

Moreover, you can say that Ruby has this principle called POLA (principle of least astonishment). In short, it means that Ruby behaves in a way that won't surprise experts. 

 

Ruby Language Uses

 

  1. Object Oriented: Ruby is a purely object-oriented programming language. Moreover, each value is an object. Every object has a class, and every class has a superclass. 
  1. Mixins: Ruby contains a feature of single inheritance only. However, we all know that Ruby has classes and modules. In short, a module has methods, but it has no instances. 
  1. Variable constants: In Ruby, if an already initialized constant is modified in a script, it will trigger a warning but will not affect your program.
  1. Keywords: In Ruby, there are approximately 42 keywords that can't be used for other purposes. 
  1. Duck and Dynamic typing: Ruby is a dynamic programming language. In addition, their programs are not compiled. However, all class, module, and method definitions are built by the code.
  2. Naming rules: Ruby has some rules for naming variables, methods, constants, and classes.
  • Constants: Start with a big letter.
  • Global variables: Begin with a dollar sign ($).
  • Instance variables: Start with an (@) sign.
  • Class variables: Begin with two (@@) signs.
  • Method names: Can start with a big letter.

Here, we’ve mentioned some features of the ruby programming language. Now, we will learn how you can set up this in this Ruby tutorial guide.

 

How to Set Up Ruby?

 

The following section will elaborate on the installation of this program on different Operating Systems. 

 

Installation Process of Ruby in Windows OS

 

  1. First, we can get Ruby from https:// rubyInstaller.org/.. Click on a link based on your Windows version. For an example of ruby language, choose WITHOUT DEVKIT versions like Ruby 2.6.4-1 (x64) for Windows (64-bit) or Ruby 2.6.4-1 (x86) for Windows (32-bit).
     
  2. After downloading, run the .exe file and follow the instructions to install Ruby on your Windows. Once installed with default settings, set up the environment variable.
  3. After that, go to Control Panel> System and Security> System
     
  4. Further, click on Advanced System Settings, then under the Advanced tab, click on Environment Variables
     
  5. Then, edit the Path variable under System variables to include the Ruby environment.
     
  6. Now, select Path and click Edit
     
  7. Moving ahead, click New, then add the path where Ruby is installed. 
     
  8. By default, it's 'C:\Ruby26-x64\bin' or 'C:\Ruby26-x86\bin'. If installed elsewhere, add that path. 
     
  9. Now, we will provide you ruby language example, on another drive, locate the Ruby folder, copy the bin folder's path, and include it in the System Variable's Path like 'SomeDrive:\SomeFolder\RubyXX-xYY\bin’.
     
  10. At last, click OK and save the settings. To check if the installation is correct, open the command prompt, type 'ruby -v’, and hit Enter. You should see output like 'ruby 2.6.4p104 (2019-08-28 revision 67798) [x64-mingw32]'. In other words, it means Ruby has been successfully installed.
     

With the assistance of the installation process, you can effortlessly learn ruby programming. 

 

To Download and Install Ruby on Linux:

 

  • First, open the Terminal by going to Applications.
  • After that, type the following command:

sudo apt install ruby-full

  • Then, press Enter and provide your password. Wait for the download and installation to complete.

That's it! Ruby has now been installed on your Linux system. To confirm that the installation process succeeded, enter 'ruby -v' in the Terminal. If you see 'ruby 2.6...', you have successfully installed Ruby on your Linux.

Now, we will learn about the syntax that will be beneficial for all users in this ruby tutorial. 

 

Ruby Syntax

 

Let's explore the basics of Ruby programming and how to use fundamental elements in your programs.

 

1. Variables:

 

In Ruby coding language, we use variables to give names to objects in our program. In other words, we use the = operator for the assignment:

amount = 5

puts amount

In this example, 'amount' is the name given to the integer object 5. Remember, a variable is just a name for an object, not the object itself.

Note: The name on the left side of = is the name assigned to the object on the right side.

 

2. Data Types:

 

Ruby's data types represent different categories of data and are implemented as classes in its object-oriented structure.

  • Strings:

Strings are sequences of characters enclosed in single ('') or double ("") quotes.

  • Numbers:

Ruby handles integers (whole numbers) and floats (numbers with decimals). Further, you can also use floats for precision.

  • Booleans:

Booleans have two values: true or false. Moreover, they use logic statements for decision-making.

Let's break down the provided code:

  • We define two variables, my_string_1 with the value "Dog" and my_string_2 with the value "Cat."
  • An if statement checks if the two variables are equal. If true, it outputs "True!"; otherwise, it outputs "False!".
  • The end keyword concludes the if statement. In this case, since "Dog" is not equal to "Cat," the output will be "False!".
  • Arrays:

An array is a data structure that stores multiple items. We create an array named our_array with various data types: strings, a float, a boolean, and an integer.

  • Comments (lines starting with #) provide explanations about the code.
  • Additionally, the comment indicates the intention to print all elements of the array.
  • Each method iterates through each element in the array, and Ruby prints them out.
  • Symbols:

Symbols, introduced by a colon (:), are lightweight alternatives to strings. Moreover, they save memory space and offer better performance.

  • About Comments:

In Ruby, comments start with the # symbol and go until the end of the line. In addition, the Ruby program will ignore anything written after the # symbol.

Remember: You can put comments anywhere in a line, not just at the beginning. They help explain the code.

 

Conclusion

 

You've finished our thorough Ruby tutorial. At this point, you should have a strong understanding of Ruby programming and feel ready to take on different projects. Additionally, keep in mind that the key to mastering Ruby is practice. Therefore, start creating your projects and delve into the wide world of Ruby possibilities.

 

Frequently Asked Questions

 
Q1. Is Java better than Ruby?

Ans. Java is a heavy-duty tool perfect for big and complex tasks, especially for bulky systems. On the other hand, Ruby is a handy tool. Additionally, it is the best for building websites, especially if you're a startup or working on new business ideas.


Q2. Is Ruby the same as C++?

Ans. C++ uses a lot of formal words and specific rules, making you write more for simple things like dealing with text or files. Whereas Ruby utilizes the human-friendly syntax, which understands each concept more precisely.

About the Author

Upskill Campus

UpskillCampus provides career assistance facilities not only with their courses but with their applications from Salary builder to Career assistance, they also help School students with what an individual needs to opt for a better career.

Recommended for you

Leave a comment