Sunday, 12 August 2012

Ruby part-2

Data types and Syntaxes:
                   Data types are many in Ruby.All data types are classes in Ruby.The most basic class is "Basic Object".Data kinds in Ruby are the following.
-->Numbers
-->Booleans
-->String
-->Array
-->Hashes.
Conditional Statements and Loops:
   The "if" loops exists in Ruby just like any other languages.Its syntax is
if condition
do this
elsif
do this else
end.

Instead of braces we use begin and end statements are used to complete the block.
The looping is done using the "each" loop.Array traversals uses loops.
Syntax:
array.each(|index| puts index)
This prints the array 5 times.
Classes and Objects:
    Syntax:
class classname
def fn_name
#function definition
end
end
There are different variables that can be put under this as explained in the previous post.
Comments:
# is used for one line comments.
=begin and =end are used for multiline comments..

Inheritance:
By default all class methods are public.All attributes are private and can be accessed using methods.
Inheritance is achieved in ruby using ">"
Syntax:
class class name>old class name
//define the class
end.
Overriding is possible in ruby.You can open a class and override any existing method,the way you want it to be.You can also include modules into your class put it in a different file and access it,just like the libraries in c.
create a module using
module module_name
#define your methods here

end

you can include module using.
require "filename" in the file where you are going to access.Then give
include module_name.
-->If you are accessing a method which is inside a module"Two" which is again inside another module "One".Then if yu have included the module "One" in your program then you have to access the method using Two.method-name.You can also define classes inside the modules and include it in other programs and create objects to it.

Proc and Lambdas:
Both are used like function calls and both are the objects to the same class.But are both the same??
No.Proc is not very strict with the number of arguments but Lambdas are very strict.So based on the requirement you can select one among them.
Syntax:
p=proc(|a,b| p a b)

p.call(1,2) r p.cal(1) will not return any error.
But if you give
p=lambda(|a,b| p a b)
the p.call(1) will return an error dur to the wrong number of arguments.

Array Operations:
There are many inbuilt operations for any class which you can access it via the "class-name".methods.You can also over ride these methods.Some of the functions are
compact-removes Null space
eql?
concat
count
cycle

The functions are used to perform their direct meaning.Method arguments are very flexible in Ruby.The arguments to these functions may be different.Because it is dynamically typed during the runtime it takes the required type.If you dont pass anything t will take an empty array,else it will take the arguments passed.You can also have methods where you need not pass any values because the default values are already given in the method definition.
Real fun begins when you mix and match the optional argument methods,Default argument methods and the runtime argument methods.

For eg give
def fn(a,b,*p)
end
The first 2 arguments will be allocated to a and b.And all others will be assigned to the array "p".
Else if it is like

def fn(a,b,*p,q)
end
Then the last argument will be assigned to "q" everything else is the same like the above method.
Same occurs when you want to over-ride any default arguments too.
Caution:
You can define a method like
def fn(a,b=19,*p)
end
but not like
def fn(a,*p,b=19)
end

because the length of "p" is undefined .


No comments:

Post a Comment