A Tale of Two Cities: Javascript and Ruby
On friday our instructors at Flatiron School mentioned that we are going to begin lectures on JavaScript (JS). I was excited because JS is the language I was trying to learn during the time I first decided to learn coding and was applying to the Flatiron School.
For the weekend we were assigned to read some material on JS and write about some of the similarities and differences between JS and Ruby.Â
Even thought I programmed my Interview project in JS, I can honestly say that I forgot the little bit of JS that I knew before becoming a fellow at the Flatiron School.
Whatâs great about learning how to code, is that once you learn the basic concepts you can always adapt to other languages. Programming languages concepts are going to be the same as long it belongs to the same paradigm.Â
Javascript (JS) us an interpreted computer programming language. As part of web browser, implementations allow client-side scripts to interact with the user, control the browser.
In short, JS is a programming language that adds behavior to your web pages and runs on your browser(client-side). JS is also part of the three musketeer of front-end world; HTML, CSS and Javascript.
Just like Ruby, JS syntax was influence by C and itâs object-oriented. One of the difference between JS and Ruby is that JS is a Front-end/Client side language while Ruby is a Back-end/server-side language.Â
Another difference is that Ruby has a block-level scoping of local variables, while JS has a function-level scoping. When it comes to objects, a JS object is a collection of properties that reference other objects while Ruby object contains a hash of instance variables that reference other objects. Ruby enforces a level of data encapsulation, every object in Ruby has an associated class.
Below i have outlined how to create variables, arrays, hashes and classes. How to create methods, which are functions in JS.
# Ruby array = ["A", "B", "C"] array.push("D") array # ["A", "B", "C", "D"] array.reverse! # ["D", "C", "B", "A"]
// JavaScript var array = ["A", "B", "C"]; array.push("D"); console.log(array); // ["A", "B", "C", "D" ] array.reverse() // ["D", "C", "B", "A"]
# Ruby Hash hash = {} hash['A'] = 1 hash['B'] = 2 hash['C'] = 3 hash.each { |key, value| puts "#{key} #{value}" } # A 1 # B 2 # C 3
// Javascript Hash var hash = {} hash['A'] = 1 hash['B'] = 2 hash['C'] = 3 for (key in h) { console.log(key, h[key]); // A 1 // B 2 // C 3
#Ruby def greet(name) puts "Hello #{name}!" end greet("Armando") # Hello Armando!
//Javascript function greet(name) { return "Hello " + name + "!"; } greet("Armando"); // Hello Armando!
# Ruby class Person attr_accessor :firstName, :lastName def initialize(firstName, lastName) @firstName = firstName @lastName = lastName end def fullName @firstName + "" + @lastName end end armando = Person.new("Armando", "Amador") armando.fullName # "Armando Amador"
# Javascript function Person(firstName, lastName) { this.firstName = firstName; this.lastName = lastName; } Person.prototype.fullName = function() { return this.firstName + " " + this.lastName; } var armando = new Person("Armando", "Amador"); armando.fullName(); // "Armando Amador"
Besides the differences between both languages, they work well with each other since they actually compliment each other. If your goal is to become a âFull-Stack Web Developerâ knowing both of these languages can take you places.Â