DEV Community

Carlos Castaneda
Carlos Castaneda

Posted on

TIL Blog Day 7 - Intro to Ruby

Over the course of a few days I worked on and finished the Intro to Ruby module. Overall it was a lot more enjoyable than Codecademy. While Codecademy is definitely a great resource to learn programming basics, I find the content can be a little dry and the site holds your hand too much. The Ruby modules on First Draft were great and it pushed you to think about what you learned when doing the graded code blocks.

I have a little over a year of learning JavaScript so I love to see some of the differences between JS and Ruby. Below are a couple of differences I noticed while working on the modules.

Truthiness and Falsiness

While working through the 'If statements' module i noticed that Ruby has less 'falsy' values compared to JavaScript.
In Ruby, only false and nil are consdered falsy values.

JavaScript on the other hand has the following falsy values.

false
undefined
null
0
-0
On
NaN
""
Enter fullscreen mode Exit fullscreen mode

Equality

Another area were Ruby differs from JavaScript is the equality operator. In Ruby you can only use

== 
!=
Enter fullscreen mode Exit fullscreen mode

In JavaScript you have access to a third equality operator.

!= // not equal to
== // loose equality, equal to but attempts to convert operands that are different types
=== // strict equality, both operands must be same type
Enter fullscreen mode Exit fullscreen mode

Using the == operator can create problems in your code

// The following code returns true
'2' == 2
Enter fullscreen mode Exit fullscreen mode

This can lead to errors in your code, so generally it is recommended to always use the strict equality operator.

While Ruby does have a === operator, it is not used for comparing equality. The === operator in Ruby checks if an object belongs to a class or range. It can be used like this:

(1..10) === 1
# => true

String === 'foo'
# => true
Enter fullscreen mode Exit fullscreen mode

Overall I really like the simplicity of Ruby. It is easy to type and it doesn't have a lot of curly braces and parentheses all over the place like JS.

Top comments (0)