Ruby Programming Language

waisttherapeuticΛογισμικό & κατασκευή λογ/κού

4 Νοε 2013 (πριν από 4 χρόνια και 8 μήνες)

99 εμφανίσεις

Ruby Programming Language

Jonathan Gertig

CSC 415: Programming Languages

Dr. Lyle

, 2012

Ruby Programming Language


The History of Ruby and The Creation of Rails

I hope to see Ruby help every programmer in the world to be productive, and to enjoy
ng, and to be happy. That is the primary purpose of Ruby language.”


Yukihiro Matsumoto 2008 Google Tech Talk on Ruby 1.9

The Metasomatism (process by which rubies are formed)

February 24, 1993 is the day that Ruby began forming. The creator Yukihiro
atsumoto stated in 2001, during an interview with Bruce Stewart, that at the time he
had just become very interested in scripting languages and wanted to find one that had
object oriented aspects. He explained that he began his search by looking into the s
beta Perl 5 which was to showcase new object oriented features, but sadly he was nun too
impressed. He moved onto Python and was impressed with its objected oriented nature
and interpretive design, but it had too many notes of procedural programming a
nd just
did not feel like it was a scripting language. Matsumoto related that he had decided on
wanting “a scripting language more powerful than Perl, and more object
oriented than
Python.” This is how Ruby was born. The name Ruby itself was just an idea o
ne of
Matsumoto's friend's had based on the fact that Ruby itself is based in large part off of
Pearl which is another type of precious stone.

The Cutting and Forming

Ruby was first publicly released December 21st, 1995 in its beta 0.95 form. Ruby

Ruby Programming Language


1.0 wa
s released Christmas day 1996. September 22nd, 1997 Matsumoto is hired to work
full time on his once pet project. Ruby 1.9 was released December 2007 with the latest
stable build at version 1.9.3. As of October 25th of this year the Ruby core team have
ounced a 'feature freeze' meaning that all features under
development for 2.0.0 that
have not already been approved by Matz will not make it in.

The Age of Rails

Rails came about from the work that David Heinemeier Hansson did on
Basecamp. Hansson first
released this full
stack web application framework for Ruby,
open source in July of 2004. Rails is a Model
Controller frame work built on the
Active record pattern as well. Since the release of Rails, Ruby which in and of it self is a
general purpose
programing language, has gained a resurgence in popularity as a great
option for agile and scalable web applications.

Data Types

I believe people want to express themselves when they program. They don't want to
fight with the language. Programming langua
ges must feel natural to programmers.”


Yukihiro Matsumoto

Standard Types

Ruby does not work in the standard coding format of naming the variable type
first, if it is of the standard data types it does not need to be named at all and therefore has

Ruby Programming Language


a f
ormat of variable name = data, for example Name = “Jon” or a = 0;


The standard types in ruby are numbers, strings, ranges, arrays, and hashes. All
programing languages deal with numbers that is how computers work. Ruby works in
such a way that nu
mbers can be up to any length as long as there is memory space. With
in the ranges of
2*pow(30) to 2*pow(30)
1 or
2*pow(62) to 2*pow(62)
1 they are held
as binary internally and are of object type Fixnum but outside of these ranges they are of
object typ
e Bignum. Ruby is one of few languages to have native support for big
numbers. Ruby also has native support for octal, hex, and binary formats the only change
required is a leading sign for octal a 0 will precede the number for instance the number
0377 wou
ld be understood to be 225 in octal. 0D is used for decimal but is default so is
not necessary, 0x is for hex and 0b is for binary. Any numeric literal with a decimal point
is assigned to object type Float


Ruby strings are just basically a seque
nce of 8
bit bytes. The escape literals for a
single quoted string, which you can use %q// to denote, are “
” and “
'”. Double quoted
strings, which you can use %Q!! to denote, have a huge amount of escape literals, most
notably would be “#{}” because it a
llows you to place any ruby code with in the string.
This is in my opinion so much better then having to concatenate the string to add in a
word dynamically. Even if the speed may be the same the syntax is better. You can also
construct a string with what
is known as a here document which would look like

Ruby Programming Language


varName <<End_of_String

anything here

and here



A range in ruby is a range object with two references to Fixnum objects this is
different to how Pearl stores ranges as lists. Ranges
can be used as sequences, conditions,
or intervals. A range being used as a sequence is the most basic use of ranges and allows
you even to have a sequence of custom objects. You can test if an object, number, string,
or whatever else is within the range

by using “===”.

Arrays & Hashes

I combined arrays and hashes sections because in Ruby and most languages they
are the same thing but only differ in the key access type. Arrays use integers as the index
location but hashes are built to allow any object a
s a key.

Ruby Programming Language


Abstract Types

Ruby's abstract data types unlike Ada or many other languages have gone like the
tide of object
oriented languages. That is there are no direct abstract data types in Ruby
what you will need to do is create an object to descri
be your new data type.

Control Structure

There are all the basic control structures in Ruby as there are in other languages.
Ruby has if, while, for, try
catch. All Ruby structures require “end” to end the block of
code. This in my sight is a down fall

for Ruby it is syntactically easier to learn because of
it but it does not feel like it is a proper closing method.

Classes and Working with Objects

Ruby Programming Language



Classes in Ruby are similar to those of Java and C# in that Ruby classes can only
have o
ne superclass. This method is strong and simple but having only one superclass is a
problem because real world objects may inherit attributes from many sources. Ruby has
dealt with this issue by what are called mix
ins or modules. Ruby classes allow for gl
variables with in classes, these variables are not instance variables and are indicated by
@@ before the name of the variable. Ruby classes like all object
oriented languages have
class methods, or methods that are available out side of an object from

that class such as a
File.delete() method.


Modules in Ruby are versatile and useful entities. One of the major uses is as a
package or library of methods that can be used by classes and be completely independent
from them. The second use is as a

in or as an additional superclass allowing the class
to access methods within the module. This allows for pieces of code that are heavily used
to be passed around easily. Classes can access a module by using the include tag be for
the name of the modu

Concurrency and Multi


Threads in Ruby are completely portable this is because they are totally
implemented with in the Ruby interpreter. This like Java means that only one core will be
used for processing and that there is a
likelihood of thread starvation.
Threads in ruby

Ruby Programming Language


work by creating a new tread function and give it a block of code to run such as this

threads = []

6.times do |number|

threads << do |i|

raise "Boom!" if i == 2

raise "W
am!" if i == 4

print "#{i}



threads.each do |t|



rescue RuntimeError => e

puts "Failed: #{e.message}"



Ruby Programming Language


Event Handling

Nativity in Ruby no event handling exists.

Error Handling

Error C

When catching errors I ruby you use a begin … end block of code in which you
tell Ruby what exceptions you want to deal with by using the

tag which is a catch
statement for exceptions. After the rescue tag you can place any number of errors

then the
code to run on the case of that exception happening. You can have as many rescue tags in
a begin … end block as you need.
If you think that you code has fixed the error you may
have have it retry. If you know your code may not fix the error you
are having you can

the error again which passes the exception up a level in the stack.

@esmtp = true


if @esmtp then





rescue ProtocolError

Ruby Programming Language


if @esmtp then

@esmtp = false






In this case the person is attempting to login to a SMTP server with one of two
ways, the first being ehlo. This code tries first to connect using the EHLO but if a
protocol error is received it switches to the HELO comm
and but if that does not work it
raises the exception.

Error Raising

The Kernal.raise command is able to do more than reraise an exception because it
is able to take parameters to be raise.

raise “
matix out of bounds”

Is a command that would raise a
run time error with “matrix out of bounds” as the error
text. You can all so name the exception or even create a new error class.

class MatrixException < RuntimeError


raise MatrixException

matix out of bounds”

Then there is the catch … throw block.
Catch … throw works by naming a
variable that if thrown all of the commands placed on the control stack after the catch line

Ruby Programming Language


are removed from the stack and the block is exited without executing any of the

catch (:done)

while line = gets

throw :done unless fields = line.split(/




With this example in the line is not formatted correctly it will trow out all progress it
made and exit the block without running



Rails comes pack loaded with neat and easy commands that you can run to get
you off you feet. For example here is how to start a new project with a basic database and
getting it running on the local server:

>rails new appName

>cd appName

ails generate scaffold User Name:string Birth_Day:Date

>rake db:migrate

>Rails Server

That easy! It generates all the needed files and connections for you and even sets up the

Ruby Programming Language


routing for you. Many of the commands even have short hand notation such as the

>Rails s

Or the scaffold:

>Rails g s User Name:string Birth_Day:Date

Of course for each generate command there is a destroy command.


There are three main files that need to be edited when working

on rails

he Model the View and the Controller. The Model file is used mostly for the
interconnection of database models. The Controller file is the main class file where all the
functions and subroutines go. The View is the HTML file that is seen in correlation to
Model it is most likely named after.

Ruby Programming Language



Ruby has great readability the control structures are clearly defined and
understandable. Where readability may come into question is anytime when dealing with
regular expressions because tr
ying to understand a regular expression with out knowledge
is like trying to read an alien language. Ruby is almost like reading a story book.


Writing Ruby code is very simple to learn there are not very many intricacies in
syntax. One of
atsumoto's goals was to make programmers easily productive using
Ruby and he did that very well.


The Ruby programing language is only as reliable as the system it is running on.
Ruby is a bit slow at time when it come to multi

or mult
icored applications.
Ruby in an of itself is a very reliable language but Rails is still moving rapidly through
the evolution of what it will become.


If you are learning Ruby as your first language I would say there is a slight
learning curve with R
uby which has mainly to do with routing and where to put what in
files. I would give learning Ruby as a first language about half a month to be putting out
functioning web applications, but if this is not your first language I would say tops a

Ruby Programming Language


week. In the

modern work place you can learn a new language better with practice and
google to find the answer to questions. There are elements used by the Ruby language
that are not implicitly a part of the language that may take some more time other than
what I have


Ruby Programming Language



Tomas, Dave.
Programming Ruby
. 2nd. Dallas: The Pragmatic Bookshelf, 2005. eBook

Matsumoto, Yukihiro.
Ruby in a Nutshell
. Chicago: O'Reilly Media, 2001. eBook.