Ruby vs JavaScript
Phase 3 at Flatiron School introduced me to a whole new language Ruby. After days of coding I can hear the chatter surrounding my fellow classmates and how they felt more comfortable with Javascript so I decided to write my second blog on Ruby and Javascript differences and similarities. Both languages were both made around the same time period 1995 but were made with different goals. JavaScript was made by Brendan Eich with the purpose of being able to run in the browser. Ruby was made By Yukihiro Matsumoto, and was designed to make programmers life easier. He wanted it to be simple and dynamic to use, easy and light to write, but capable of building complex things quickly. Javascript is a client-side coding language that excels at front-end application development. On the other hand, Ruby on Rails is a full-stack framework that is most often used for backend development. It’s obvious from this description that they don’t compete, but rather complement each other. Ruby is a modern, dynamically typed coding language that is specially designed to make scripting and app development not only easy, but also fun to work on. It utilizes a full stack, and superbly optimized web framework known as Ruby on Rails framework. It’s generally used for backend development.
Let’s start of with some basic syntax differences, “puts” is a built-in Ruby method that will output a string of text to the terminal. It’s the Ruby equivalent of console.log
You can also see in this image that in Ruby, any line that starts with a “#” won’t be executed. This is the Ruby equivalent of “//” in JavaScript.
Like JavaScript, Ruby lets you define strings with either single quotes or double quotes:
If you want to use string interpolation in Ruby, use double quotes like so:
In Ruby, unlike JavaScript, there are two types of numbers: Integers and Floats.
Integers are whole numbers, like 4
Floats are decimal numbers, like 4.4
In Ruby, there is one special value that represents the absence of a value, nil.
In JavaScript, there are two different data types for representing the absence of value: “null” and “undefined”
Booleans
There are only two values of the Boolean data type: `true` and `false`. In Ruby, however, there is no such thing as a Boolean class. Instead, every appearance, or instance, of `true` and `false` in your program are instances of `TrueClass` and `FalseClass` respectively:
In Ruby, only nil and false are falsy values. Everything else is truthy, even 0 and empty strings.
Contrast this with JavaScript, where null , undefined, false, 0, NaN, an “” are all falsy values.
One of the first things you likely learned in JavaScript was how to write a function. In Ruby we will write methods see the difference below.
Ruby has slightly different syntax for writing conditional statements using `if/else` then JavaScript. Here’s an example below `if/else` statement in both JavaScript & Ruby
I spent much of my time above showcasing the differences/similarities in the above codes. They are two different pieces of code that basically accomplish the same thing. As mentioned before both of these simple programs do essentially the same thing. This covers some of the differences and similarities so i wont go on to explain the rest because hopefully by now you get the point, both of these codes are done in different languages using different methods, but despite all that their end goal is the same.