Page Content

Tutorials

Introduction To Ruby, Why It Is Used, Versions And Set Up

Yukihiro Matsumoto, popularly known as Matz, created Ruby, a dynamic, object-oriented, and interpreted scripting language. Development began in February 1993, and the first alpha version was made public in December 1994. It supports imperative, functional, object-oriented, and reflective programming paradigms and is inspired by languages such as Perl, Python, Smalltalk, Lisp, Eiffel, and Ada.

Introduction to Ruby

Ruby’s class library is effectively an object-oriented rearrangement of Perl’s features, making it a viable alternative to scripting languages like Python and Perl. Enhancing code readability was a primary design objective of Ruby, which prioritized the demands of programmers and treated the language as a user interface with usability standards. In order to reduce confusion for seasoned users, it is also associated with the “principle of least surprise” (POLS). Dynamic typing, garbage collector-based autonomous memory management, and strong object-orientation where everything is an object are important aspects.

Ruby’s first alpha version was made public in December 1994, after development started in February 1993. Matsumoto wanted to make programming faster and easier while maximizing programmer satisfaction by developing a language that put human demands ahead of computer needs. His main goal was to make sure that every feature including object-oriented ones operated as typical programmers would anticipate.

A number of other programming languages serve as major sources of inspiration for Ruby:

  • Perl: The majority of Perl’s functionality is reorganized into an object-oriented format in its class library.
  • Python: Matsumoto claimed that even though Ruby doesn’t directly take many features from Python, he was able to develop it by reading Python code.
  • Smalltalk: Like Smalltalk, it is a language that is entirely object-oriented.
  • Lisp
  • Eiffel
  • Ada
  • C Language and Perl for its syntax inspiration.

Several programming paradigms are supported by the language, such as imperative, functional, object-oriented, and reflective programming. The language was viewed as a user interface with usability criteria, and improved code readability was a primary design objective from the start. The “principle of least surprise” (POLS) is also credited with reducing uncertainty for seasoned users.

Ruby versions

The following important Ruby versions:

  • Ruby 1.6.8 was released on December 24, 2002.
  • The release date of Ruby 1.8 was August 4, 2003. The book “Programming Ruby” by Dave Thomas and Andy Hunt described this version, which was a major revision from the original edition. Ruby 1.8 included a standard documentation system (RDoc) and RubyGems.
  • The release date of Ruby 1.9 was December 25, 2007. YARV (“Yet Another Ruby Virtual Machine”) and the original MRI (Matz’s Ruby Implementation) interpreter were combined to create a new reference implementation that compiles to bytecode in Ruby 1.9. Significant changes were also brought about by it, such as support for multibyte character encodings and other API modifications. The “green threads” of Ruby 1.8 are replaced with native threads with a global interpreter lock in Ruby 1.9.
  • Ruby 2.0 was released on February 24, 2013.
  • Ruby 2.1 was released on December 25, 2013.
  • Ruby 2.2 was released on December 25, 2014.
  • Ruby 2.3 was released on December 25, 2015.
  • Ruby 2.4 was released on December 25, 2016.
  • Ruby 2.5.1 was released on March 28, 2018.

Ruby has become more and more popular, especially in web development, with frameworks like Ruby on Rails.

What is ruby used for?

Ruby is used for web development (especially with the Ruby on Rails framework), automation and scripting, DevOps, web scraping, and static site generation. As a general-purpose language, it is also used for data processing, building web servers, and creating various applications, including desktop and database-driven web applications.  

Ruby is suitable for various applications

Web Development: Ruby is used extensively in professional software development, especially for web applications, with frameworks like Sinatra and Ruby on Rails. Simple web apps can be easily created with Sinatra.

GUI Applications: The “Shoes” package for Ruby makes it easier to create graphical user interfaces with multimedia elements, enabling the production of engaging and interactive multimedia products with minimal effort. OpenGL, Tk, GTK, wxRuby, and RubyCocoa are other GUI toolkits that are supported.

Scripting and System Administration: Ruby is useful for automating processes and creating command-line applications. It frequently integrates concepts from other programming languages to address practical issues. It has the potential to be an effective “glue” language for communicating with the OS.

Learning Programming: Because of its easy-to-understand syntax and capacity to include benefits from other languages, Ruby is regarded as a suitable option for novices. Through practice and repetition, it develops skills with the goal of giving increasingly complicated subjects a “programming black belt” basis.

Ruby Setup

Installing Ruby and configuring the required tools are the first steps in setting up a Ruby development environment.

Installing Ruby

Operating Systems: Many Linux distributions and Mac OS X come with Ruby preinstalled. Although it is not preinstalled on Windows, it is readily available.

Checking Installation: By typing ruby -v into your terminal or command prompt, you may see if Ruby is installed and what version it is.

Mac OS X: Using tools like RVM (Ruby Version Manager), rbenv, Fink, MacPorts, or Homebrew, users can opt to install a more recent version of Ruby, as it comes preinstalled.

Linux/Unix: Package managers such as apt (Debian/Ubuntu) or yum (CentOS, Fedora, openSUSE) are frequently used to install Ruby. As an alternative, you can download and compile the source code.

Windows: The RubyInstaller (One-Click Installer), which may be found at rubyinstaller.org, is the suggested method. Making sure the “Add ruby executable to your PATH” option is selected during installation is essential in order to enable the execution of Ruby commands from any directory. Cygwin may be used to install Ruby and offers a Windows environment similar to Unix.

Ruby Version Managers (RVM, rbenv): These command-line utilities make it easier to install and manage various Ruby versions and related gemsets. They make it possible for developers to seamlessly transition between Ruby versions for various projects.

Essential Development Tools

Text Editor: Writing Ruby code requires a plain text editor. Many popular editors, such as Notepad++ (Windows), TextWrangler (Mac OS X), or gedit (Linux), have syntax highlighting to make code easier to read.

Terminal/Command Line/Shell: You will use this to interface with your operating system and run Ruby apps. Basic commands such as ls (list files), cp (copy files), rm (delete files), cd (change directory), and pwd (print working directory) must be understood.

Interactive Ruby Shell (IRB): Ruby includes a Read-Eval-Print Loop (REPL) called IRB that lets you experiment with code snippets and run Ruby statements instantly without generating a file. Entering irb in your terminal initiates an IRB session.

RubyGems (gem): This package management system for Ruby makes it easier to install and manage third-party libraries, or “gems.” The gem install [gemname] command is used to install gems. RubyGems is included with Ruby 1.9, however it must be installed individually for Ruby 1.8.

RDoc (rdoc, ri): Ruby code documentation is done with these tools. To browse documentation for Ruby classes, modules, and methods, use the ri command-line tool. Ruby source code documentation comments can be extracted and formatted into HTML with rdoc.

Rake: Running unit tests, creating documentation, and creating gem packages are just a few of the repetitive chores that may be automated using this Ruby build tool.

Running Ruby Programs

  • Files ending in.rb are commonly used to store Ruby programs.
  • In your terminal, type ruby filename.rb to start a script.
  • Adding a shebang line (#!/usr/bin/env ruby) as the first line of a script and using chmod +x filename.rb to provide it executable permissions will make the script self-executable on Unix-like systems.
  • The -e option can also be used to run simple Ruby programs straight from the command line, such as ruby -e ‘puts “Hello World”‘.
  • Expect to see error warnings when you first start; it’s crucial to learn how to interpret and comprehend them. To prevent “No such file or directory” issues while launching files, make sure you are in the correct directory.

Your First Ruby Program: “Hello World!”

A fundamental first program in many languages, including Ruby, is “Hello World!” This demonstrates how to print text to the console. In Ruby, the puts method is commonly used for this.

Code Example (ex1.rb):

puts "Hello World!"
puts "Hello Again"
puts "Govindhtech Solutions."
puts "It was started from 2023 ."
puts "It's an Online learning hub."
puts "Founder of the company was Agarapu Ramesh."
puts 'It publish valuable content .'

To run this program, you should type the text into a file named ex1.rb (Ruby files typically end in .rb). Then, from your terminal or command prompt, navigate to the directory where you saved the file and execute it by typing ruby ex1.rb. It is important to be in the correct directory to avoid a “No such file or directory” error.

Output

Hello World!
Hello Again
Govindhtech Solutions.
It was started from 2023 .
It's an Online learning hub.
Founder of the company was Agarapu Ramesh.
It publish valuable content .
Agarapu Geetha
Agarapu Geetha
My name is Agarapu Geetha, a B.Com graduate with a strong passion for technology and innovation. I work as a content writer at Govindhtech, where I dedicate myself to exploring and publishing the latest updates in the world of tech.
Index