Pages

An Introduction to Ruby - part 1

Ruby is red hot today.Rails based applications sprung up as most hosts began to support the framework.What makes interesting the object oriented concepts getting implemented in Ruby.Ruby is being used world-wide for text processing, XML and web applications, GUI building, in middle-tier servers, and general system administration. Ruby is used in artificial intelligence and machine-learning research, and as an engine for exploratory mathematics.Its becoming powerful in its infancy..

Ruby is a dynamically typed language ie the programmer neednot define the type of variable.A programming language is said to use dynamic typing when type checking is performed during run-time (also known as "late-binding") as opposed to compile-time.The interpreter takes care of the storage class at run-time based onthe value held by variable.Yukihiro Matsumoto is the creator of the Ruby programming language began work on Ruby back in 1993, because he wanted a language that made him productive while being fun to use. Initially popular in Japan, Ruby has been finding its way into the hearts of programmers all over the world.

The conventional Hello world:
print 'hello world'

Cool.No main method.No class definitions... to run a simple program ! Lesser code less time for debug mode.
No semicolons at the end of line unless multiple statements in a line.
Calling a method is simple a like method(args) or method arg1,arg2.
Everything you manipulate in Ruby is an object (even the numerals), and all methods are invoked in the context of an object.

To print 'hello world' ten times:
10.times{"hello world" }

What about null object ? Its 'nil' in Ruby. It is also an object. So we can call methods of nil object like nil.methods.

Ruby class definitions are remarkably simple: the keyword "class" followed by a class name, the class body, and the keyword "end" to finish it all off. Ruby features single inheritance: every class has exactly one superclass. A class with no explicit parent is made a child of class Object–the root of the class hierarchy, and is the only class with no super-class.In Java the Object class is the super class of all classes by default.

Names that start with single “at” signs (@) are instance variables.Unlike languages such as Java and C++, you don’t have to declare your instance variables in Ruby; they spring into existence the first time to reference them.

Instead of constructor it uses initialize method to initialize its values.

class.new(params)

This new message allocates an empty, uninitialized object, and then sends the message initialize to that object,passing along any parameters that were originally given to new.

Although a class may have only one parent class (the single inheritance model), it may mix in any number of modules. This effectively gives Ruby the power of multiple inheritance without some of the ambiguities that can arise.

eg:

class Animal
def initialize( speciesname )
@speciesname = speciesname
end
end

man = Animal.new( "Homo Sapiens" )

class Omnivores<> def initialize( speciesname,food )
super(speciesname)#initialize super class
@food = food
end
end

manall=Omnivores.new("Homo Sapiens","All")

Defining a method is simple:

def method(arg1,arg2)
#statements
end

The methods return a value.If no return is specified the value of last statement is the returned.More than one value can be returned as it bundled as an array.

Modules provide a way to indirectly support multiple inheritance in ruby.A module included in a class is called a mixin.
With a mixin you can extend from a module instead of a class.A module can’t be instantiated and no class can directly extend it but a module can fully implement methods like an abstract class does.

module Math
def add(a, b)
BigInteger.new(a+ b)
end
end

Ruby has its powerul unique expressions.expressions in the right hand side of the assignment operator are evaluated in the order in which they appear beore the statement is made to the variables in the left.

To swap two variables:
a,b = b,a

No comments:

Post a Comment