Ruby Basics(Noob Friendly)

urfriendlyheckerman

New member
Jun 2, 2020
2
0
0
Hey there! I just wanted to make this little basic guide for beginners, yes it will be public. So to get started we'll be talking about; printing things, taking user input, interactive console, variables, constants. This is just some basic stuff to get you started on Ruby 2.8 and interested in the language :)

Printing to the terminal/console

Okay so there are two ways to print to the terminal in ruby, and that is puts and print
Example:
puts "Hello World!"
print("Hello World!\n")
As you can see in the above examples, there is something clearly different about these two. Let me explain:
So puts as you can see doesn't need brackets to function, nor do you need to add the newline escape character (\n), it adds it automatically.
Whereas print does not, so you will need to add this in yourself.

Creating Variables and Constants

Creating variables and constants is allot easier than you think.
Variables in ruby must start with a lowercase letter or an underscore, anything else would be invalid.
Variables can also be changed in methods, classes and within the main scope of the code(basically the body, anywhere, where a class or method isn't defined).
Example:

car = "Nissan"

Printing the variable:

puts car

But what if we wanted to print it with a string already? Then we do this:

puts "Your car is a #{car}"

Confusing, right? Eh you'll get it over time, just be sure not to mix the two!

We have now created a variable and is ready to use.
Now lets get onto constants, constants are always and only defined with a capital letter, they can only be defined outside of methods.
A Constant is used when a value isn't supposed to change, but Ruby doesn't prevent that unlike other languages.
Even though Constants shouldn't be changed, it can still happen on accident, you will get an error but your program will work just fine. A good thing to do is to force it to be unchangeable by using .freeze after your Constant.
Examples:

CAR_PRICE = 2000

Now we can still change this, the best fix is this:

CAR_PRICE = 2000.freeze

Now if you try change this Constant, it will give an error.
Printing the Constant:

puts CAR_PRICE

Taking User Input

Okay now time to learn how to take user input, we will be utilizing what you have learned with variables :)
Now there are two ways with this as well, but both are considered the same in some sense it is, gets and gets.chomp
I will explain the difference between these easily, so with .chomp it just removes any newlines, whereas gets captures those as well. So basically gets.chomp is allot cleaner and easier to manage.
Examples:

puts "Hi! What is your name? "
name = gets.chomp


See how we used our variable created to store the input from the user. Now whatever the user entered will be infact stored in name and is ready to use :)

Interactive Console (IRB)

The IRB is an interactive console that lets you run Ruby code like live! So you don't have to save files and all the the other hassles.
But its only useful for testing and stuff like that, if you want to create a full on program it is not ideal. Use this for testing your skills and debugging.
All you have to do is type irb in your terminal and follow it with your Ruby code and you're good to go!

Well that was my beginner friendly Ruby tutorial, I hope you enjoyed it :)
 
Thread starter Similar threads Forum Replies Date
V Learn Ruby Beginners guide Java , Python & Ruby 2
Similar threads