An Introduction to Ruby  for Javascript Devs
Tags: beginners, javascript, ruby
Published: 2020-07-19 14:30:00

**SOME BACKGROUND**

Ruby is "a dynamic, open-source programming language with a focus on simplicity and productivity.  It has an elegant syntax that is natural to read and easy to write," according to its docs on ruby-lang.org. Developed in 1995 by Osaka native Yukihiro Matsumoto, who "really wanted a genuine object-oriented with easy-to-use scripting language" as an alternative to Python.  Focused on "developer happiness," Ruby is a beginner-friendly language that was intended to be written more closely to human language than its predecessors, using verbs like 'puts' and 'do' for keywords. 

Ruby is most commonly implemented with Rails, a framework that extends the language and provides structure and scaffolding to make writing code quicker and easier.  It is considered an *opinionated* framework, as there is typically only one correct way to do a task, making it potentially easier for beginners to learn than a language/framework that has many different ways to achieve the same results. Ruby on Rails works on the backend of an application to fetch from databases and display data that contains HTML, CSS, and JS.  With its database-driven design, model-view-controller (MVC) architecture, and built-in testing, Ruby on Rails allows for maximum productivity- one language to rule them all.  Many of today's most popular websites were built on the Ruby on Rails framework, including GitHub, Airbnb, Groupon, Hulu, Soundcloud, and Kickstarter.

**RUBY VS JAVASCRIPT**

Ruby and JavaScript are both object-oriented programming languages that were developed in the same year.  Matsumoto designed Ruby with developers' satisfaction in mind, famously stating his intent was to 'help every programmer in the world to be productive, and to enjoy programming, and to be happy.'  JavaScript's main goal was to be a programming language that could be run easily and efficiently in web browsers.  They are both common first languages for beginners, and both are popular choices for coding bootcamps, but Ruby is often considered easier to learn, mostly because of its brevity, structure, and simpler syntax.

Every programming language must provide ways to iterate over data.  Let's check out some simple while loops:

```javascript
//JavaScript

let x = 1;
while (x <= 10) {
  console.log(`The number is ${x}.`);
  x++;
}

//Ruby 

x = 1
while x <= 10 do 
  puts 'The number is #{x}.'
  x += 1
end

```

Pretty similar looking, but Ruby's syntax uses verbs that we really use in English, allowing us to tell at a glance what action each line will perform.  

A key difference between the two languages is that Ruby is a true class-based language.  Although with ES6, JavaScript adopted some keywords like 'class' and 'new' to make it look familiar to programmers coming from other languages, JS is really a classless language.  That means that in Ruby, objects are created directly from a class, whereas JavaScript objects are actually created from prototypes.

```javascript
//JavaScript

class Cat = (name, breed, treats) {
  this.name = name;
  this.breed = breed;
  this.treats = treats;
  this.receiveSnacks = () => {
    treats++;
    console.log(`Enjoy your ${treats} snacks, ${name}, 
you chubby ${breed}!`);
  }
}

garfield = new Cat('Garfield', 'orange tabby', 3);
garfield.receiveSnacks();

//a prototype done with an object constructor


//Ruby

class Cat 
  def initialize(name, breed, treats)
    @name = name
    @breed = breed
    @treats = treats
  end

  def receive_snacks
    @treats+= 1
    puts 'Enjoy your #{@treats} snacks, #{@name}, 
you chubby #{@breed}!'
  end
end 

garfield = Cat.new('Garfield', 'orange tabby', 3)
garfield.receive_snacks 

//a class made with class keyword and initialize method
```

The class keyword in JavaScript is simply syntactic sugar designed to obscure away some of the complexity of its inheritance methods.

Overall, the languages are overwhelmingly more similar than they are different, and if you have a solid understanding of JavaScript, you should have few problems adapting to Ruby's ways. You may even find that its simplicity saves you a little time.

**CONCLUSION**

There's a lot of talk online in developer forums about whether Ruby's heyday is over, but the truth is that it doesn't appear to be going anywhere.  It did enjoy a resurgence in popularity in the early 20-teens which has since waned a bit, but the language was recently given some tweaks that increased its already-great performance significantly, with a major update just released in May of this year.  The language continues to evolve in response to trends and feedback, remaining a solid choice for developers who want to write concise and clean code that is readable and powerful.