Ruby on Rails vs. JavaScript
Tags: beginners, ruby, javascript, webdev
Published: 2024-02-05 14:16:31

## Intro
As a beginner to software development, I want to explore the many different programming languages available. Today we’ll be taking a look at one of the most popular beginner friendly programming languages, Ruby on Rails. We’ll be looking at what RoR is, what it’s used for, it’s architecture, and how it compares to my main programming language, JavaScript.

### What is Ruby on Rails?
In [“What Is Ruby on Rails, And What Is It Used For?”](https://www.codingdojo.com/blog/what-is-ruby-on-rails#:~:text=Ruby%20on%20Rails%20(RoR)%20is,websites%2C%20apps%2C%20and%20systems.) Brad Mitchell describes RoR as ‘open-source full-stack framework software specifically to build different web applications.’ Ruby on Rails can be broken up into 2 parts: Ruby and Rails. Ruby is the all-around programming language and Rails is the framework that is utilized for web development and app creation. Ruby is a primarily back-end language but works with HTML, CSS, and JS for the front-end. This is due to the architecture of Ruby on Rails. One of the main reasons RoR is so popular is because a lot of the work is done for you and is simplified for the programmer. Ruby also have 2 philosophies to follow: DRY (“Don’t Repeat Yourself) and “convention over configuration” or CoC. CoC is to make programming easier as long as you follow the conventions of the language. 

### Ruby on Rails Architecture
Ruby on Rails follows the Model-View-Controller pattern. The model component interacts with the database and manages the data that will then be passed to the view component. Guessing from the name, this component pertains to anything that will be visible to the user. With the view component falling on the front end, we will incorporate HTML, CSS, and even Javascript into our code to make a dynamic and presentable website or application. Lastly is our controller component that ties everything together. The controller component “receives data from the model component and passes it to the view component” as said in [‘What is Ruby on Rails?’](https://intellipaat.com/blog/what-is-ruby-on-rails/#:~:text=types%20of%20content.-,Ruby%20on%20Rails%20Architecture,and%20maintain%20complex%20web%20applications.) by Ashish Thakur.

Let’s take a look at an example of a model in Ruby.

```
# == Schema Information
#
# Table name: users
#
#  id                  :integer          not null, primary key
#  aspiring_occupation :string
#  bio                 :string
#  display_name        :string
#  email               :string
#  employer            :string
#  occupation          :string
#  password_digest     :string
#  created_at          :datetime         not null
#  updated_at          :datetime         not null
#
class User < ApplicationRecord
  validates :email, :uniqueness => { :case_sensitive => false }
  validates :email, :presence => true
  has_secure_password

  has_many(:ai_messages, { :class_name => "AiMessage", :foreign_key => "user_id", :dependent => :destroy })

  def prompt
    <<~TEXT
      
    TEXT
  end
  
end
```

In the above image, we can see how simple it is to interact with the database. When I said that a lot of the work is done for me, this is a glimpse into that. I did not type most of that file. Majority is what was generated for me when I passed the data model code as a command in the terminal. When creating a User model, Ruby on Rails knows that we will need to verify the user's information so each User model will automatically know to verify the user when signing in.

```
<div class="my-3">
  <div>
    <h1 class="sr-only">
      Message details
    </h1>

    <div>
      <div>
        <a href="/ai_messages">
          Go back
        </a>
      </div>


    </div>
  </div>
</div>

<hr>

<div class="card" >
  <ul class="list-group">
    <li class="list-group-item"><%= @user_message.content %></li>
    <li class="list-group-item"><%= @assistant_message.content %></li>
  </ul>
  <div class="card-footer">
    <%= time_ago_in_words(@user_message.created_at) %> ago
  </div>
</div>

<hr>

<div class="my-3">
  <a href="/delete_ai_message/<%= @user_message.id %>">
    Delete Message
  </a>
</div>
```

Here in the view component, we have mainly HTML but instead of using script tags to have our backend interact with the front like we do in JS, we have embedded ruby tags that specify where exactly we want these pieces of memory to interact.

```
def create
    @user = User.new
    @user.email = params.fetch("query_email")
    @user.password = params.fetch("query_password")
    @user.password_confirmation = params.fetch("query_password_confirmation")
    @user.occupation = params.fetch("query_occupation")
    @user.bio = params.fetch("query_bio")
    @user.display_name = params.fetch("query_display_name")
    @user.employer = params.fetch("query_employer")
    @user.aspiring_occupation = params.fetch("query_aspiring_occupation")

    save_status = @user.save

    if save_status == true
      session[:user_id] = @user.id
   
      redirect_to("/ai_messages", { :notice => "User account created successfully."})
    else
      redirect_to("/user_sign_up", { :alert => @user.errors.full_messages.to_sentence })
    end
  end
```

In this controller component we can see how the model and view components intermingle. Certain aspects of our controller manage data from our model and direct to the view (look for the word ‘template’ followed by an arrow pointing to a html.erb file which is an HTML page with embedded ruby).

### What is RoR used for?
Ruby on Rails can be used for making a multitude of programs, such as social network apps, content-management systems, and project management tools. Ruby has produce such versatile results with the help of RubyGems. Morganne Gagne describes Ruby gems as “open source libraries that contain Ruby code and are packaged with a little extra data” in her blog [‘What is a Ruby Gem?’](https://medium.com/@morgannegagne/what-is-a-ruby-gem-1eec2684e68). These can be compared to libraries like Moment.js, Underscore.js, or jQuery.

### Ruby on Rails vs JavaScript
One of the biggest benefits of Ruby is how clear it is to read. Ruby is known for how easy it is to read and it’s built in framework that makes it easier for beginners to create web applications, but how does it compare to JavaScript?

RubyGems allow a lot of work to be simplified but one of the downsides of the gems is that some conflicts can arise. With an over reliance of Ruby gems, your gems can become incompatible. Gems are created by developers in the Ruby community so conflicts are a possibility. On the other hand, JavaScript libraries don’t have the issue of affecting the performance of another. 

A huge difference between Ruby and Javascript is that Ruby is a compiled language while JavaScript is an interpreted language. Compiled languages translates the program into machine readable code. This can result in your program taking up a lot of memory. Interpreted languages produce a result from running the code, this means that you can run JS in your browser and see real time changes.

While Ruby on Rails is one of the most-popular amongst newbies, JavaScript is one of the most used programming languages across the world with “98.4% of websites using JavaScript”, stated by [Zach Paruch](https://www.semrush.com/blog/javascript/).

## Conclusion
Ruby on Rails is a great introduction to coding with clear syntax, simplified code conventions, an expansive library and interactive framework to show you the wonders of web development. In fact, Ruby on Rails was the first programming language I learned and the code examples you see are from my first application that I created only 3 months into coding. Ruby on Rails being my first language made programming less scary and learning JavaScript easier due to their similarities. I say all this to say that Ruby on Rails are not competitors but more like teammates and they have their own specific benefits that make them great for learning.