Ruby is a scripting language which is need no be compiled. The code will be interpreted. It supports Object Oriented programming. Even the basic data types are objects. It is dynamically typed as in Javascript. It is a server side scripting language.
The class of each object can be known using the ".class" command.
For example : 10.class will return Number.
Similarly "10".class will return Fixnum.
Each class is associated with member functions. For example we can convert a string object into a Fixnum using the to_i method.
The methods can either be for a class or for an instance of the class. The class methods will have self keyword. They can only be accessed by the class name. The instance methods will be accessed by the instance names.
Similar to the constructors in the C++. there is a special method called as initializer in the Ruby. This method will be invoked whenever a new instance is created.
There are private and public variables too like c++. But they have different syntax.
There are access specifiers such as attr_reader which allows public read of the particular variable. attr_writer which allows public write of the variable. There is attr_accessor which allows the variable to be both read and written in public.
Containers :
The container objects available in the language are array and hash. We should specify the key and value pairs in for the hash. If we want to access the value all we need to do is use the key to access the variable.
The syntax for the array is ,
a = [1,2,3,4,5]
and for a hash,
h={"1"=>prem,"2"=>anandh}
In the above we can access like,
puts a[0]
puts h["1"]
The output is,
1
prem
Control Structures and loops:
The control structures include if(condition), elsif(condition) statements doing the job of the if(condition) and the else if(condition) of the C++. There is an option called as unless structure. The working
if(condition) unless( ! condition )
#some_code #some_code
end end
If the condition is true in if the #some_code will execute and the opposite is there in unless.
There is a syntax called as
case (option) when match1 when match2 end
This is similar to the switch case in ruby. It is a multi branching statement unlike if else.
The loops in ruby are,
for, while and until. The first two are familiar as they are similar to the loops in C++. The for loop has syntax like,
# If you're used to C or Java, you might prefer this.
for element in collection
...
end
# A Smalltalk programmer might prefer this.
collection.each {|element|
...
}
Coming to until .
until (condition1)
....
end
In the until loop the loop will continue till the condition is true.
There are options for loops like:
5.times {p "*" } #print "*" 5 times.
3.upto 6 {p "*" } #prints "*" 3 ( 3 to 6) times.
Classes and objects:
Classes have their name preceded by the "Class" keyword. We can create the objects using the "new".
object = Class_name.new
Inheritance:
Inheritance in ruby is done by the "<". Here there is a base class
class some_class < parent_class
...
end
Here the class some_class is inherited from the parent class.
Procs and Lambda and Blocks:
Well i might call Procs as generic functions which can be passed as parameters and then they could be called from there. The syntax and working of these are listed at the site http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/
Procs and Lambda are similar but the only two differences between them are,
- Lambda's check the number of parameters getting passed.
- Lambda's let the method execute even after it has returned a value but Procs don't. Procs will return the control the out of the function once it returns the value.
A block is also a Proc.
Scope in Ruby :
Name Begins With | Variable Scope |
---|---|
$
| A global variable |
@
| An instance variable |
[a-z] or _
| A local variable |
[A-Z]
| A constant |
@@ | A class variable |
The above mentioned table contains the table having the ruby variables and their scope. For clarifications in details visit http://www.techotopia.com/index.php/Ruby_Variable_Scope
Ranges :
Ruby allows a new concept of ranges where you can specify the start and end of the range and you the intermediate values will be automatically generated.
Ranges can be used as conditions, intervals and sequences.
For example :
if marks == 0..49
puts "fail"
else
puts "pass"
Thus the above code will give "fail" is the mark is between 0 and 49. There are two ways of creating ranges.
using "start..end" and "start...end". The first range is from start to end and the second one is from start to value_previous_to_end.
Iterators :
Ruby offers two iterators. They allow us to traverse through the collections. They are .each and .collect.
.each will not return any individual element while the .collect returns a new collection.
For example :
ary = [1,2,3,4,5]
ary.each do |i|
if( i%2==0 ) puts i
end
The output is,
2
4
And for the .collect operator.
ary = [1,2,3,4,5] new_ary = ary.collect{ | x | 10*x }
puts new_ary
The output is,
10
20
30
40
50
Thus we can manipulate the values in the original array and return the new array to new_array.