Syntactical Introduction To Ruby language

Tehleel Mir
8 min readOct 15, 2022
Photo by Jason D on Unsplash

This article is only for people who know at least one programming language, if you are not one of them, go to Mr. White to learn some chemistry.

What you will learn from the below 1835 words?

You will learn nothing, new in programming, but you will learn the syntax of the ruby language. With which you can start writing and running…

create a file with .rb extension, write some stuff (ruby) in it, open terminal on that same folder and type -> ruby fileName.rb to run that file.

Btw this will only work if you have ruby installed in your system

…ruby code.

I don’t have anything else in my mind to write more, so here is how to write rubbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbby

We will go just like google search.

  • How the hell can I write/add comments in ruby?

In Ruby, there are two types of comments as like most of the language

single line && multi-line

  • How can I print stuff on the console in ruby?

Well, there are two methods for it.

1. use puts but it will add a new line on the terminal

2. use print and it won’t add any new line.

  • How do Variables work in ruby?

Well, just like the 5 sons of Robert Baratheon, in ruby we have 5 different types of variables.

This is what they say who knows how many bastards are there

And btw ruby is a dynamically typed language, so variable type is checked at run time, so just like freaking JS, no data type is needed in ruby for a variable declaration.

  1. The Local Variable: will be only accessible within a specific block, like an if statement or in a function. And it must start either with a lowercase letter or with an underscore.
  2. The Instance Variable: this type of variable is object-specific i.e. it only belongs to one specific object of a certain class. And for some weird reason, it must start with @ symbol. And it can be only declared in a class.
  3. The Class Variable: Kinda similar to an instance variable but with some disorders, first to declare it you have to use the double @ symbols, and again can only be declared in a class only. The main difference between the two is class variables are shared between a class and all its subclasses, while class instance variables only belong to one specific class. Didn’t get it I know try this, Class variables are shared by all “class instances” (i.e. subclasses) whereas class instance variables are specific to only that class. But if you never intend to extend your class, the difference is purely meaningless.
  4. The Constant Variable: This one never changes its value (just like your ex), and to declare it you must start the variable name with an UPPER case letter.
  5. The Global Variable (this one is like a queen on the chess board, bitch can be accessed from anywhere): so yeah as I said it can be accessed anywhere from any class, file, or whatever. And to declare it start the variable name with a $ sign. And they say you should never use global variables, then why the hell are they there?
  • How can I perform the casting of variables in ruby?

That is how we can cast one data type to another type like an integer to float and so on. Well, it's pretty simple actually, you just have to remember a few functions.

  1. To convert a data type to an integer use to_i
  2. To convert a data type to float use to_f
  3. To convert a data type to a string use to_s
  • How do Strings work in ruby?

Everything in ruby is an object because it's 100% object-oriented language (remember this, why? I don’t know it seems important), there are no primitive types or anything related, everything is an object. So when we create a string whether that be a variable or direct a string value…

nums = “string”

or

“string”

…its an object, and there are various methods that we can use/apply to the string objects. And if a function or method doesn’t have any params we can call a function without brackets (). Strings can be defined either with double quotes “hi” or single quotes ‘hi’.

  • Number data type in ruby

And just like String, we can perform certain actions on Integer/Number objects as well, especially arithmetic

  • How to take user’s input in ruby from the console?

As of now, you know how to print stuff on the console but do you know how to take stuff in from the console? Of course, you don't know, you know nothing, Jon Snow.

what I mean is to take the user's input from the console. Just like we have puts method to print stuff on the console in the same ways we have gets method to get the stuff from the console. Few important points to note here

  1. gets method will always return a string no matter if the user inputs a float or integer value it will always be a string.
  2. when the user will press enter after entering some stuff, gets will take that enter as a new line (\n) and add it to that string.
  3. To get the user input without that new line we use gets.chomp method
  • Arrays in ruby

I will show the syntax in a bit, but first (what the hell are you doing with your life), let's talk about the array implementation or properties you can use in ruby. So, as I said ruby is a dynamically typed language, it won’t only allow us to change the data type of variables at run time by assigning different data type values, but we can also create an array with different data type items in it, if you are familiar with JS you know what the hack I'm talking about. And there are also various methods that we can use on array objects and also we can create 2d 3d or nD arrays as well. (what does that D reminds you btw?)

  • How to define functions/methods in ruby?

Now in ruby, there are various ways to (call — create) a method. And method names should start with a lowercase letter otherwise compiler might think that it's a constant and hence can parse the call incorrectly. Methods should be defined before calling them, otherwise Ruby will raise an exception (which we will see later how to handle those bad boys) for undefined method invoking.

We can also define default params for functions and also variable number parameters and can also return more than one value from a function. And if we don't return any value from the function, the last line or the statement will be returned by default. And if the method doesn't have any parameters we can ignore the brackets () both while defining the function and while calling it.

Methods start with a def keyword and then the method name and the method block ends with the end keyword. In Ruby, there are no curly brackets {} to start a code block everything starts with a keyword like if, while, switch, def, and so on and ends with the end keyword. Except for the Ruby blocks where we can use { } for single-line statements.

When a method is defined outside of a class definition, its by default marked as private. On the other hand, when it's defined in the class it's marked as public by default. Whenever you want to access a method of a class, you first need to instantiate the class. Then, using the object, you can access any member of the class. Ruby also gives you a way to access a method without instantiating a class.

Now into the code:

  • Conditional statements in ruby

Well, they are pretty straight just have a look. (Dormammu, I’ve come to bargain)

  • What is Dictionary in ruby?

Well, it's obviously not an Oxford dictionary, It's nothing but a simple map or hashmap, where we save the key-value pairs. A map/dictionary can have duplicate values but the same is not true for keys. Each key must have a different value. But each key can be of different data types and the same is true for the value as well. (same thing different name blah blah..)

Here is the syntax:

Do you see that green follow button?

its there for a reason, you know?

you know?

  • Loops in ruby

Well in ruby you also get multiple types of loops, you have a while loop, for loop, for each loop, and also a weird one.

  • How to handle Exceptions and errors in ruby?

(yoga might help), Remember I told you we are going to handle those bad boys later, I mean the exceptions that a code can throw. In ruby, we also have to try a catch type of system but with a different keyword name. For try, we have begin and for the catch, we have rescue (pretty clever name tbh), and the whole block end with the end keyword.

We can also throw out our own exception using the raise keyword and we can also have multiple rescue blocks to catch the specific exceptions.

  • How to create classes in ruby?

To define a new class, we have a class keyword for that which must be followed by the class name itself, and the whole class block ends with the end keyword.

We can define our own class members like function and variable and also instance and class variable which I mention in the variables topic.

There is a special function by the name of initialize, it's nothing but a simple constructor function, i.e. it will get called as soon as the object of the class gets created.

To define the attributes/properties of the class we use attr_accessor followed by the : and the variable/attribute/property name.

And to create the object of the class we use new keyword on the class name. And it will return the object/ instance of that class.

We can also perform inheritance from class to class. For that, we have to use < sign followed by the class name which we want to inherit.

You will find all the examples in the code snippet below:

  • What are Symbol in Ruby

Symbol is a unique instance of a symbol class, that can be created by : followed by the symbol name. A symbol is used to identify a specific resources like a method, variable, or map/hash key.

One main thing is that symbols are immutable i.e. you can not give new value to them and also if you create a new symbol with the same name it won’t create a new instance rather then it will point to the same old symbol object which you might have already created in the code before. (Under the hood it's just using the same memory pool for it, nothing fancy)

You can find all the code examples -> here <-

As I said this is just the basic start-up guide to start writing code in ruby, of course there is much more stuff to learn in ruby (which I myself don’t know yet) which you can learn online after understanding the above basics.

For the game of thrones fans, that mistake was intentional.

Bye.

--

--