♦️ Basics of Ruby! ♦️


Ruby is a really cool and not that hard to learn language, it’s got a syntax sort of like a mix between Lua and Python but still unique and quirky. It’s fun to use and you can do a lot of really cool stuff in it.
First off, we have comments.
Comments, just like in python, can start with a # symbol. Like this

But unlike python, we can also have multiline comments in ruby. These multiline comments start with =begin and end with =end

They look like this

In ruby, there are two ways to print something to a console. We can use print and puts
puts writes to the console and then makes a new line, while print doesn’t.
So if we did

The console would print

But if we did

We would get

In the console. Print is handy for things like prompts for getting input from the user and such.
Another note, you may sometimes see p putting things to the console, p works similar to puts, but puts whatever is in front of it, the differences look like this

So don’t get them confused, puts is not the same as p

We can also add together strings

And we can add strings and numbers as well, as long as the numbers are converted to strings, which we can to be adding .to_s to the end of them (to string)

We can also put numbers inside of strings by using #{} like this

There are five main types of variables in Ruby, just like most other languages, they are integer, boolean, string, float, and list.
You don’t need to declare what type the variables are, Ruby automatically figures that out.

Getting input from the user

Now that we have an idea of how to put stuff to the console, here’s how to get an input back from the user.
We use gets which gets the input from the user, and then chomp to chomp that input, it looks like this

We can use print as a prompt since it won’t start a new line

Now you can use just plain gets you don’t need to chomp but if you don’t chomp it then it will also return the line break at the end, so you input will look something like “name\n” instead of just “name”

While loops

While loops work just like in python, you give it a argument and as long as that is true it will continue to do the code underneath it.
Just something to note with Ruby, you don’t put parenthesis around your arguments or use brackets like in some languages, and you don’t use : like in python. You simply leave nothing, and put the keyword end at the end of the block of code which the loop will execute

if, else, and elsif

If and else statements take a argument, and if the argument is true then they do the following code, end keywords are also put at the end of if/else statements.

Notice that you don’t put the arguments inside of parenthesis and you also don’t use : or {}

elsif works almost the same

Just always remember to put end at the end of any indented block of code.

Lists and Loops

You can do a lot of cool stuff in ruby with lists, and although it seems a little complicated at first, it’s not that hard and easier then other languages.

In other languages, such as python, we might do something like this

Or in Javascript

But in ruby we just use the .each operator (I think that’s what it’s called)
Which looks like this:

Let’s break this code down.
.each is used to reference each item in the list. do tells us what we are gong to do, and |item| inside of the vertical lines is what we are calling the items inside the list, this doesn’t have to be item, it can be whatever we want, its just a reference to the item in a list, this is often just i or n

Below that line we put what we want it to do with each item, which is put it to the console, and then we put end, to signify that we are ending our loop.

Times Loops

.times loop is a pretty cool loop we can use to do something a certain amount of times. And it looks like this

We just put the number we want, and then .times to signify that we are going to do the line of code below that many times.
We can also reference the number, by doing something like this

This will result in 0-9 being printed out, because computers start with the number 0 and go up from there.
This can be a little bit annoying, so if we want it to go from 1 to 10 instead of 0 to 9, we need to do this


Sort of like dictionaries in python, hashes in ruby store data with keys and values.
There are two ways to create a hash, we can create a hash be giving it values, or we can create a hash using Hash.new

Hash.new is for creating an empty dictionary, it looks like this

Hashes have a default value, which is nil so if we try and reference a value with a key that does not exist in a hash, it will give us nil, unless we create a different default value. Maybe we want the default value to be Value not found we can put this as a default value in a hash by putting it inside parenthesis in the .new part of creating a hash, like this

When creating hashes in ruby, we can use strings as keys, using the rocket syntax, like this:

But in newer versions of ruby we use tokens instead of strings as key names because they are faster in processing.
A token starts with : and then has the name of the token, :token

We can use rocket syntax with tokens in ruby

However in the newest versions of ruby, we don’t have to use rocket syntax anymore with tokens, instead of putting a rocket symbol => we put the : at the end of the token name and then the key value, just note that when referencing a key still put the : at the front of the token. It looks like this:


Methods in ruby will remind a lot of people of python functions because they have a very similar syntax. A method starts with the keyword def short for define, and then the method name, and any arguments in parenthesis.
Underneath the method name and indented goes any code that will be completed when the method is called, and the method ends with the keyword end
Here’s an example of a simple method to add two numbers

If there are no arguments for a method then we just put empty parenthesis ()

Other stuff

Well, that’s pretty much the basics of ruby (Although I’ve probably forgotten something)
But here’s some other random stuff that is good to know in ruby.

Clearing the screen

Using colors

Requiring other stuff

Just like in python there are hundreds of different packages to do pretty much whatever you want in ruby. You add them to your code by using require and then the name of the package in ””
But before you require something you have to make sure it’s added to your packages folder in your ruby files, to do this just type gem install package_name in the console.
On replit you need to go to packages on the left side of the screen and search for it, then press the plus to add it.

That’s pretty much all you need to know to get started with the basics of ruby.

Oh yeah, and just to show you the cool stuff you can do with ruby, below is a little password guesser I made, it's not exactly the fastest thing in the world, but it will eventually guess passwords.... eventually

You are viewing a single comment. View All

@IntellectualGuy #SelfPromotion :)