Ruby vs. JavaScript
Tags: webdev, javascript, ruby, codenewbie
Published: 2023-10-09 13:21:02


![Literal Ruby shining](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4zdd49mp3euilv4hnn45.jpg)

One of the myriad advantages of becoming fluent in a language like JavaScript is the ability to transfer those skills to other languages. It's almost the equivalent of learning Latin as your base language. You can understand romantic languages such as Italian, Spanish, or French more easily with the Latin base. The same goes for programming. Learning the ins and outs of one language will make it easier to pick up another language. Today we are comparing Javascript and Ruby: the similarities and differences between Data Types and Functions, and the advantages of each language.
  
Note: in this blog, I assume you are familiar with JavaScript terminology. 

#DATA TYPES
**Similarities**: 

Strings, Numbers, Mathematical Operators (+, -, *, /, %), Arrays that are 0 indexed. Identifiers are names of variables and are case-sensitive. Identifiers can use alphanumeric and underscore characters.

**Differences**: 
_Javascript example: Ruby example_

Null: Nil, Ruby's null. 
Objects: Hashes. Hashes look similar, but they are versatile with their keys as they can be any data type. Symbols, which are immutable strings, are copied by reference similar to objects and the symbols can be used as keys. Javascript allows us to pass functions as values for objects, which allows for greater functionality compared to Ruby's hashes. 
Comment out: '#' for Ruby, // for JS.

#FUNCTIONS
**Similarities**:

console.log(): puts. Ruby has different versions of a printing function: puts, print, and p. puts will add a new line automatically after it is invoked. print will not add a new line and continue to use the current line. p is useful for debugging as it will display a more 'raw' form of data that includes linebreaks.

```Ruby
puts "Hola World!"
# OUTPUT: Hola World!

print 123
print 456
print 789
#print continues on same line
123456789

puts 123
puts 456
puts 789
#puts gives every message its own line
123
456
789
#also affects arrays differently
puts [1, 2]
1
2

print [1, 2]
[1, 2]

```
**DIFFERENCES**
Variable Declaration

Local Variables must start with an _ or lowercase letter. They are local to the method or code block they are declared in. 

Global variables start with $ and are accessible across different classes. Global variable assignments alter the global status and are not recommended. They can make debugging difficult.

Class variables start with a @@ sign. They are accessible by the child objects of the class. 

Ruby functions are called Methods. Ruby uses the keyword def to declare a method and end to finish the method.

```Ruby
class Example
  def method
   local = 'variable1'
   _local = underscore 1 'variable2'
   end #end of method
   @@numberoflocals = 2 #class variable 
end #end of class
$global_variable = 'Hello World!'
```


BEGIN and END block. Code you put in here will unconditionally execute either before or after anything else when the file loads, regardless of the position in the interpreter. Code executes in a top-down function. Similar to JS but we have hoisting of functions and var keywords
```Ruby
puts "This is the main Ruby Program"

END {
   puts "Now we Terminate the Ruby Program"
}

BEGIN {
   puts "First we Initialize the Ruby Program"
}
# produces:
# First we Initialize the Ruby Program
# This is the main Ruby Program
# Now we Terminate the Ruby Program

```





#ADVANTAGES OF EACH

**JavaScript Advantages**
-Application
Javascript leans toward front-end programming, focusing on client-side applications, server-side, and browser interaction and changes. 

-Speed
Due to its optimized engine, Javascript can be up to 20 times faster than Ruby in some cases

-Need
JavaScript accounts for the basis of over 97% of all websites and because of this, there is always a demand for Javascript Developers.

**Ruby Advantages**
-Application
Ruby leans toward back-end programming where we can generate HTML pages that run server-side and interact with the database.

-Learning Curve
As Lane said in his blog, [(https://blog.boot.dev/javascript/ruby-vs-javascript-which-language-should-you-learn-first/ )]



> If your main objective is to pick up an easy, fun language and work in backend development, then Ruby is a great choice. Ruby helps build good programming habits, and is truly a language built for beginners and laying down a solid foundation for you to learn more languages later on.

#Conclusion

At the end of the day, whichever language you decide to learn will only further your familiarity with coding concepts, patterns, and applications. Once you understand what to look for and how to translate these skills to other languages, picking up a new language will not only be fun but increase your worth as a developer. Javascript, Ruby, C++ and so on all have advantages and disadvantages that a skilled developer can navigate. I hope that you will too. 

![Ruby from Steven Universe](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8mdaf9uw5bq4650pb04o.jpg)
**Sources**
https://www.educba.com/javascript-vs-ruby/
https://medium.com/@asiddiqui0692/are-hashes-in-ruby-the-same-as-objects-in-javascript-53bf7e4949c1
https://www.rubyguides.com/2018/10/puts-vs-print/
https://www.educba.com/javascript-vs-ruby/
https://blog.boot.dev/javascript/ruby-vs-javascript-which-language-should-you-learn-first/
https://www.codecademy.com/resources/docs/ruby/variables
https://www.tutorialspoint.com/ruby/ruby_classes.htm