What is Ruby?

Ruby is an object-oriented scripting language originally developed by Yukihiro Matsumoto (also known as Matz). The main website of the Ruby programming language is ruby-lang.org. Development began in February 1993 and the first alpha version of Ruby was released in December 1994. It was developed to be an alternative to scripting languages such as Perl and Python. Ruby borrows heavily from Perl and the class library is essentially an object-oriented reorganization of Perl's functionality. Ruby also borrows from Lisp and Smalltalk. While Ruby does not borrow many features from Python, reading the code for Python helped Matz develop Ruby.

Installing Ruby

MacOS comes with Ruby already installed. Most Linux distributions either come with Ruby preinstalled or allow you to easily install Ruby from the distribution's repository of free software. You can also download and install Ruby on Windows. The more technically adept can download the Ruby source code[2] and compile it for most operating systems, including Unix, DOS, BeOS, OS/2, Windows, and Linux.

Contents

  1. String Literals
  2. Program Flow
  3. Input and Output
  4. Objects
  5. Classes
  6. Numbers
  7. Arrays
  8. Functions
  9. Ruby on Rails
  10. References
String Literals

One way to create a String is to use single or double quotes inside a Ruby program to create what is called a string literal. We've already done this with our "hello world" program. A quick update to our code shows the use of both single and double quotes.

Try it out!

puts 'Hello world'
Program Flow

Ruby contains methods to implement conditional branching, just like any other OOP language.

Ruby includes a pretty standard set of looping and branching constructs: if, while and case. For example, here's if in action:

a = 10 * rand if a < 5 puts "#{a} less than 5" elsif a > 7 puts "#{a} greater than 7" else puts "Cheese sandwich!" end
Input and Output

Ruby provides what at first sight looks like two separate sets of I/O routines. The first is the simple interface---we've been using it pretty much exclusively so far.

Have a go!

print "Enter your name: " name = gets
Objects

After months of work, our highly paid Research and Development folks have determined that our jukebox needs songs. So it seems like a good idea to start off by setting up a Ruby class that represents things that are songs. We know that a real song has a name, an artist, and a duration, so we'll want to make sure that the song objects in our program do, too.

initialize is a special method in Ruby programs. When you call Song.new to create a new Song object, Ruby creates an uninitialized object and then calls that object's initialize method, passing in any parameters that were passed to new. This gives you a chance to write code that sets up your object's state.

class Song def initialize(name, artist, duration) @name = name @artist = artist @duration = duration end end
Classes

Ruby is a perfect Object Oriented Programming Language. The features of the object-oriented programming language include − Data Encapsulation Data Abstraction Polymorphism Inheritance These features have been discussed in the chapter Object Oriented Ruby. An object-oriented program involves classes and objects. A class is the blueprint from which individual objects are created. In object-oriented terms, we say that your bicycle is an instance of the class of objects known as bicycles. Take the example of any vehicle. It comprises wheels, horsepower, and fuel or gas tank capacity. These characteristics form the data members of the class Vehicle. You can differentiate one vehicle from the other with the help of these characteristics. A vehicle can also have certain functions, such as halting, driving, and speeding. Even these functions form the data members of the class Vehicle. You can, therefore, define a class as a combination of characteristics and functions.

Class Vehicle { Number no_of_wheels Number horsepower Characters type_of_tank Number Capacity Function speeding { } Function driving { } Function halting { } }
Numbers

Under the hood, for reasons that are mostly technical, there are actually different kinds of numbers: For example there are integer numbers (those without a fraction, i.e. a dot and decimal places), and depending on their size there are two kinds of them. For floating point numbers there are even more. Unless you are super curious, you can perfectly ignore all of that for now, and just think of numbers as numbers. However, when you do calculations with numbers, keep in mind that integer numbers (“integers”) and decimal point numbers (floating point numbers, aka “floats”) are different. If you do a calculation that uses integer number you’ll always get an integer back.

Arrays

Creating an Array To create an array in a Ruby program, use square brackets: ([]), and separate the values you want to store with commas. For example, create an array of sharks and assign it to a variable, like this:

sharks = ["Hammerhead", "Great White", "Tiger"]
Functions

The Ruby language makes it easy to create functions. Function Syntax: def functionname(variable) return (value) end

Ruby on Rails

Ruby on Rails, or Rails, is a server-side web application framework written in Ruby under the MIT License. Rails is a model–view–controller (MVC) framework, providing default structures for a database, a web service, and web pages. It encourages and facilitates the use of web standards such as JSON or XML for data transfer, HTML, CSS and JavaScript for user interfacing. In addition to MVC, Rails emphasizes the use of other well-known software engineering patterns and paradigms, including convention over configuration (CoC), don't repeat yourself (DRY), and the active record pattern.[4] Ruby on Rails' emergence in the 2000s greatly influenced web app development, through innovative features such as seamless database table creations, migrations, and scaffolding of views to enable rapid application development. Ruby on Rails' influence on other web frameworks remains apparent today, with many frameworks in other languages borrowing its ideas, including Django in Python, Catalyst in Perl, Laravel and CakePHP in PHP, Phoenix in Elixir, Play in Scala, and Sails.js in Node.js.

References

Official API Documentation is found here.