RUBY

Ruby Tutorial

Table of Contents

Ruby is a true object-oriented programming language. It is developed in 1993 by Yikhihiro Matsumoto of Japan. Ruby has characteristics like:

  1. It’s an open source and is free source available on the internet but it is subject to licence.
  1. Ruby is considered as a general-purpose and interpreted programming language.
  1. Ruby could be called as object-oriented programming language.
  1. Ruby is integrated into Hypertext Markup Language
  1. Ruby is employed of writing the common gateway Interface scripts.
  1. It is even going to be embedded into the hypertext Mark up language
  1. It’s a clean and far simpler syntax that allows a new developer to learn very quickly and easily.
  1. It’s significantly scalable and large collection programs written in Ruby are easily maintainable.
  1. Ruby  has massive collection of built-in functions which uses directly into Ruby scripts.
Features of Ruby

Dynamic typing and Duck typing:

Ruby is considered a dynamic programming language. The programs aren’t compiled. Entire the class module and method definition to be built by code when run.

Ruby Environment Setup:

This is about arranging up local environment setup for Ruby Programming language. Ruby installation setup on Linux/Unix. If we’ve planned to go on our development environment on Linux/Unix machine. Before going further we’ve to ensure that to have root privilege. By downloading the zip file of Ruby’s latest version. After downloading the Ruby archive, Unpack it and alter it into the newly created directory

$ tar -xvzf ruby-1.6.7.tgz

$ cd ruby-1.6.7

Now compile and configure the Ascii source code as 

$ ./configure

$ make

When finally  install ruby as

$ sy -l root # become a root user

$ make install

$ exit       # become the own or actual user 

Once this is often installed we‘ve to confirm whole is functioning by fine by issuing the subsequent command like

$ruby -v

ruby 1.6.7 (2002-06-04) [i386-netbsd]

Ruby Installation on Windows-

There are some tips  to place in or install Ruby on windows

Download the zip file of the latest version of Ruby. Once the downloading is finished the Ruby archive unpack it and also change the newly-created directory. Double click the Ruby 1.6.7 exe file. The Ruby installation wizard starts. Select the next important information page of the wizard and keep moving till the Ruby installer completes installing Ruby. There are some environment variables to set up that aren’t done properly. If we use windows 9x and add all the lines to the c:\autoexec.bat. We should set PATH=”D:\(ruby install directory)\bin;%PATH%”.

After installation see that all is working fine by issuing the following command as 

$ruby -v

ruby 1.6.7

Interactive Ruby(IRB)

Interactive Ruby provides a shell for experimentation. In this IRB shell we may do the  expression results line by line immediately. This tool always installs Ruby so we won’t be having extra to try to do to possess IRB working. Type irb at the command prompt interface and an interactive Ruby session may start as given below:

$irb

irb 0.6.1(95/04/16)

irb(main):001:0> def hello

irb(main):002:4> out = “Hello dear”

irb(main):003:4> puts out

irb(main):004:4> end

nil

irb(main):005:0> hello

Hello dear

nil

irb(main):006:0>

Consider an example, a sample program of Ruby all Ruby program have an extension .rb. The source code is put in test.rb file follows as

#!/usr/bin/ruby -w

puts “Hello, HiRuby!”

Ruby identifiers

Identifiers are called as name of variables, constants and methods. Ruby identifiers are case-sensitive. That means that ram and RAM are two different identifiers in Ruby. Its names consists of alphanumeric characters and also underscore (_) character.

Reserved words:

The reserved words are:

BEGIN, END, do, else, alias, and elsif, next, nil, true, then, undef, unless begin, ensure, false, redo, rescue, until, when, module, defined?, super, _LINE_, in, if, for, while, case, class, in, self, super, retry, false.

Here Document in Ruby

This Here Document in Ruby has with respect to creating the strings for various multiple lines. Following a<<  we will notify a string or an identifier to shut the string literal its current lines up to terminator are the values of the string. For example

#!/usr/bin/ruby -w

print <<EOF

   This is the primary kind of making

   here document ie multiple line string.

EOF

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share this article
Subscribe
By pressing the Subscribe button, you confirm that you have read our Privacy Policy.
Need a Free Demo Class?
Join H2K Infosys IT Online Training
Enroll Free demo class