Incomplete Basics from Ruby
I think the first weirdest thing in this article is the title. I thought to name this article as “Basics of Ruby”. But, this is not explain what I am planning to this article. My ideia here is to provide the very first touch in Ruby, but considering the reader is already a developer or, at least, he/she is already used to a programming language. And, specially I am able to re-read here some basic stuff about the Ruby as I am a beginner in this world too. According to Wikipedia, Ruby is “interpreted, high-level, general-propose programming language”, which reminds me a lot about Python (I got a special spot for Java and Python, judge me if you want), and for me, it is definitely a good start.
Variables
So, let’s start with variables. They are used to store values of objects through the operation of assignment, and they may be reassigned, in case of need. The types available in Ruby are string, integer, float, boolean, and nil. A small example of the usage of some variables is presented below, even though I not quite sure if the age of Captain America is correct. Probably not, but it serves for this purpose.
hero_name = "Captain America"
hero_age = 97
is_mjolnir_worth = true
sword = nil
Classes, Functions and Methods
Besides storing values into variables, the developer may want to create their own objects. Everything in Ruby are objects, and we can define them using classes (besides the types presented earlier). An example of the creation of a Hero is presented below:
class Hero
attr_accessor :name, :age
def initialize(name, age)
@name = name
@age = age
end
end
Any instance of Hero
, have two attributes: a name
and an age
. Both attributes may be accessed and modified, as the reserved keyword attr_accessor
is used to qualify them.
Arrays
As Python with lists, dictionaries and tuples, in Ruby, we have arrays. Arrays are a collection of objects, any kind of objects. It may be strange to see different type of objects in the same list (or array).
heroes = ['Capitan America', 'Hulk', 'Black Widow', 'Iron man']
empty_array = Array.new
only_true = Array.new(3, true)
range_on_different_types = [1, "two", 3.0]
Control Flow
Actions demand decisions. And decisions are made based on facts. This may be true, but sometimes you need to check if a specific information has an expected value. Ruby uses the if keyword to check if an expression is true, and then execute a code snippet inside this if scope.
if thanos.gauntlet_gems == 6 then
thanos.snap_fingers()
end
Error Handling
Nothing is perfect and errors may appear in the place. In Ruby, error handling comes to the rescue and uses a syntax that I particularly found very interested. To handle errors, Ruby separates in two scopes, the normal flow and the exception flow. Every raise keyword presented in the normal flow represents that an exception is thrown and an error is signalled that way for the error handler. That error handler is exception flows marked by rescue keyword that marks the exception code snippet to recover the program from the error that happened.
begin
raise 'Something went wrong!'
rescue
puts('I came to rescue!')
end
In this post, I do not intent to be very comprehensive in the language, but in a slight way, to present and review basic concepts, in an incomplete way.