I have just finished the last class of the "Le Wagon - Programming for Everybody" online learning series. This was my very first dip into the world of Ruby... and I must admit, I was surprised to discover how developer friendly and humanly readable this language is.
Why Ruby
You might be wondering, why did a professional developer decide to join a beginner course about Ruby? First of all, a little bit about myself. I am a self-taught developer and I got a job as a front end developer after learning how to code on Freecodecamp, Codecademy and just building projects.
I wanted to experience a structured learning course with a real instructor, where I could ask questions not only about the syntax, but also about best practices and why Ruby does things the way it does. By the end of the course, I also realised that learning a language like Ruby can make me a better JavaScript developer.
Ruby and JavaScript: some observations
Object Oriented Programming
Ruby is an Object Oriented Language, which means it manipulates programming constructs called objects. If you type "Ruby"
, you are creating an instance of the class String. As a class instance, your string will inherit methods like .length
, .capitalize
etc., that you can use to manipulate your string.
JavaScript, on the other hand, is not a class-based Object Oriented language, but with prototype based inheritance and constructor functions, it has ways of using Object Oriented Programming (OOP).
Data types
To determine what type of value something is (a number, a string, an array), in Ruby you append .class
at the end of the object, since you want to find out the class of the object it's called on. There are classes like Integer, Float, String, Array, Hash... even True and False are their own class!
true.class
# TrueClass
false.class
# FalseClass
In JavaScript, you use typeof
before your data, and this will return whether that element is a string, a number, a boolean, an object etc.
typeof true
# boolean
typeof false
# boolean
Method naming conventions
In Ruby, I saw for the first time methods ending with "?", a naming convention used to indicate that a specific method will return either true or false. For example, if you have a hash (a key value pair data structure) with tv show titles and ratings, you can use the simple method .key?
to check if the hash contains a certain key.
tv_shows = {
"Friends" => 10,
"Community" => 10,
"The Witcher" => 9
}
puts tv_shows.key?("Friends")
In JavaScript, functions can't end with punctuation but can be named semantically so that their name will describe well what they do. If you are curious to know how you can check if an object has a specific key in JavaScript, this is how you do it:
const tv_shows = {
"Friends": 10,
"Community": 10,
"The Witcher": 9
}
tv_shows.hasOwnProperty("Friends")
There is more than one way to do something
Ruby is built for usability. I was surprised to discover that for example the methods .map
and .collect
do exactly the same thing and that collect was just created in order to make Ruby feel more intuitive.
Where to go from here?
I plan on continuing learning Ruby until I am able to contribute to my company's projects that use this language. That's how I would like to reinforce this initial knowledge and put it into practice:
- Codewars kata
- Build a scraper - apparently this can be done only with a few lines of code in Ruby
- The Odin Project learning track
Top comments (0)