What is Ruby Programming Language – Basics

Ruby – Basics

Ruby Programming Language : Ruby is the interpreted scripting language for quick and easy object-oriented programming. It has many features to process text files and to do system management tasks (as in Perl). It is simple, straight-forward, extensible, and portable.
I need to mention, it’s totally free, which means not only free of charge, but also freedom to use, copy, modify, and distribute it.


Features of Ruby

  • Ruby has simple syntax, partially inspired by Eiffel and Ada.
  • Ruby has exception handling features, like Java or Python, to make it easy to handle errors.
  • Ruby’s operators are syntax sugar for the methods. You can redefine them easily.
  • Ruby is a complete, full, pure object oriented language: OOL. This means all data in Ruby is an object, in the sense of Smalltalk: no exceptions.
  • Ruby needs no variable declarations
  • Ruby features OS independent threading. Thus, for all platforms on which Ruby runs, you also have multithreading, regardless of if the OS supports it or not, even on MS-DOS!!
  • Ruby is highly portable: it is developed mostly on Linux, but works on many types of UNIX, DOS, Windows 95/98/Me/NT/2000/XP, MacOS, BeOS, OS/2, etc



How to Install Ruby and start Ruby

First, we’ll need to download ruby for windows. The easiest way to get ruby on windows is to use the Windows Ruby installer available on rubyforge. Install ruby by running the installer. Now we can test the installation. Create a test application

e:\programs\ruby

ruby -v

When you run this ruby script you should get the following output:

ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]

Working with Ruby

Create a ruby Script
For e.g.:
Puts” Welcome to interesting world of ruby”

To run ruby scripts just go to command line

> ruby hello.rb

Ruby and Excel

Here is the way to access an XLS sheet using ruby code.  The reason behind accessing excel using Ruby is Excel can be used to create data-driven Automated Test Cases.

require ‘win32ole’
connection = WIN32OLE.new(‘ADODB.Connection’)
conn_string = ‘Provider=Microsoft.Jet.OLEDB.4.0;’
conn_string << ‘Data Source=D:\working_ruby\RubyCheck.xls;’
conn_string << ‘Extended Properties=Excel 8.0;’
connection.Open(conn_string)
recordset = WIN32OLE.new(‘ADODB.Recordset’)
recordset.Open(“select * from [Sheet1$];”, connection)
data = recordset.GetRows #.transpose
len =data.length()
puts “No of Records”+len.to_s
counter = 0
while (counter<len)
puts data[counter][0] #+data[counter][1]+data[counter][2]+data[counter][3]
counter = counter +1
end
connection.close
require ‘win32ole’
connection = WIN32OLE.new(‘ADODB.Connection’)
conn_string = ‘Provider=Microsoft.Jet.OLEDB.4.0;’
conn_string << ‘Data Source=D:\working_ruby\abc1.xls;’
conn_string << ‘Extended Properties=Excel 8.0;HDR=No’
connection.open(conn_string)
recordset = WIN32OLE.new(‘ADODB.Recordset’)
recordset.open(“select * from [Sheet1$];”, connection)
data = recordset.GetRows.transpose
connection.close

 

Steps to use:

1. Open the file.

2. Save it in .rb formate. Just change the <<file name>>.txt to <<file name>>.rb

3. Change the path of your XLS file in order to access its contents

Ruby and Oracle

Most of the Application under Test (AUT) is having some relationship with Databases.  So it is necessary to understand Oracle Connectivity with Ruby.   This document deals only with Oracle but with Ruby we can connect with various Databases available in Market. There are a couple of libraries out there that will let you connect with Oracle, but it seems like oci8 is the most up to date option.
Download ruby oci8 from oci8 on rubyforge
Under 1.0.3(Version) click on ruby-oci8-1.0.3-mswin32.rb .
Download into the local machine
Note: All though there is OCI8 gem available we are not using it.

and run the installer as below in the command prompt   E:\temp>ruby ruby-oci8-0.1.13-mswin.rbCopy OCI8.rb to e:/programs/ruby/lib/ruby/site_ruby/1.8/DBD/OCI8Copy oci8.rb to e:/programs/ruby/lib/ruby/site_ruby/1.8Copy oci8lib.so to e:/programs/ruby/lib/ruby/site_ruby/1.8/i386-msvcrtOK?Enter Yes/No: yesCopying OCI8.rb to e:/programs/ruby/lib/ruby/site_ruby/1.8/DBD/OCI8 … doneCopying oci8.rb to e:/programs/ruby/lib/ruby/site_ruby/1.8 … doneCopying oci8lib.so to e:/programs/ruby/lib/ruby/site_ruby/1.8/i386-msvcrt … done
OK

 

Data base connectivity using ruby

Here is the way to access an Database using ruby code.

require ‘oci8’
conn = OCI8.new(‘scholwhs’, ‘scholwhs1’, ‘152.159.89.92:1559/l2prdd2t’)
connstring =”select location_name_short from gv_location a, g_loc_rel b
where b.school_year=’30-JUN-08′ and rel_tree_code = 2
and a.location_key=b.location_key and b.location_key_parent=’11409′
and a.location_subtype in (‘Component District’,’Non-component District’)
order by location_name_short”
puts connstring
cursor = conn.exec(connstring)
while r = cursor.fetch_hash()
puts r[‘LOCATION_NAME_SHORT’]
end
cursor.close
conn.logoff

 

Steps to use:

1. Open the file.

2. Save it in .rb formate. Just change the <<file name>>.txt to <<file name>>.rb

3. Please use your own queries



OCI & DBI-Advantages and Disadvantages

Another method for database access in ruby is Ruby DBI. Ruby DBI is a database independent interface for accessing databases, similar to perl’s DBI.

Note that Ruby DBI seems pretty unsupported; the latest change on the changelog is dated September 7th 2003. Also, Ruby oci8’s website (you need it for dbi) says that support for dbi is experimental. So, this is not really a combination which you would want to use in a production environment