DEV Community

Cover image for Ruby on Rails vs. JavaScript
allyn
allyn

Posted on

Ruby on Rails vs. JavaScript

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?” 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?’ 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
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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?’. 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.

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.

Top comments (2)

Collapse
 
nancytechprep profile image
Nancy-techprep

Thanks for sharing this, looking forward to more content!

Collapse
 
svoop profile image
Sven Schwyn • Edited

The title doesn't really make much sense, it's literally comparing apples with pears. But you explain this, RoR (framework) and JS (programming language), in the text. However, you got one important thing wrong. Both Ruby and JS are both interpreted, Ruby is not a compiled language. (Btw, this distinction is about to shift a little e.g. due to WASM, but for now, the most Ruby-like compiled language would be Crystal.)