Download - Ruby basics || updated
RUBY BASICS - II
l 19/12/11
Loops Iterators & Blocks Array Hashes Iteration over Array and
Hashes Symbols
Abstract of Session
l 19/12/11
Loops in Ruby are used to execute the same block of code a specified number of times until some condition is met.
Loops ?
l 19/12/11
While Loop
While loops will execute all of the statements contained within them as long as the conditional statement remains true.There are 3 ways to use while loop
Syntax :
1) while condition docode
end 2) code while condition
3) begin code end while condition
l 19/12/11
What is difference ? In 2nd or 3rd case, code is executed once before conditional is evaluated.
example :
num = 0max = 5
while num < max puts("Inside the loop #{num}") num += 1
end
Example
l 19/12/11
Executes code when condition is false.
There are 3 ways to use until loop
Syntax : 1) until condition do code end 2) code until condition
3) begin code end until condition
until
l 19/12/11
For Loop
l example :
• for num in 0..5l puts("Inside the loop #{num}")• end
l O/P :
• Inside the loop 0• Inside the loop 1• Inside the loop 2• Inside the loop 3• Inside the loop 4• Inside the loop 5
l 19/12/11
times
l example :
• 5.times do |index|l puts("Inside the loop #{index}")• end
l O/P :
• Inside the loop 0• Inside the loop 1• Inside the loop 2• Inside the loop 3• Inside the loop 4
l 19/12/11
What are iterators ?
l Another word for loop. In ruby, Iterators are nothing but methods supported by collections.
l Objects that store a group of data members are called collections. In Ruby, arrays and hashes can be termed collections.
l Iterators return all the elements of a collection, one after another. We will be discussing two iterators here.
• each• collect
l 19/12/11
each
l Syntax :• collection.each do |variable|
l code• end
l Example :
• num_array = [1,2,3,4,5]• num_array.each do |num|
l puts num• end
l You always associate the each iterator with a block. It returns each value of the array, one by one, to the block. The value is stored in the variable num and then displayed on the screen.
l 19/12/11
collect
l The collect iterator returns all the elements of a collection.
l Syntax :
• collection = collection.collect
l The collect method need not always be associated with a block. The collect method returns the entire collection, regardless of whether it is an array or a hash.
l example :• num_array = [1,2,3,4,5]• new_array = num_array.collect{|num| 10*num}• puts new_array
l 19/12/11
What are blocks?
l A block consists of chunks of code.
l You assign a name to a block.
l The code in the block is always enclosed within braces {}.
l A block is always invoked from a function with the same name as that of the block. This means that if you have a block with the name test, then you use the function test to invoke this block.
l You invoke a block by using the yield statement.
l 19/12/11
Blocks
l Syntax :• block_name{
l statement1l statement2l ..........• }
l Example :• def test
l puts "You are in the method"l yieldl puts "You are again back to the method"l yield• end• test {puts "You are in the block"}
l 19/12/11
Proc
Proc objects are blocks of code that have been bound to a set of local variables.Call a proc by using the variable’s call method
For Example :p = Proc.new { |x, y, z| puts 100 * x + 10 * y + z } p.call 14, 9, 2 => 1492
l 19/12/11
Lambda
Lambdas is Proc objects with some differencesCall in similar way like a proc.
For Example :l = lambda.new { |x, y, z| puts 100 * x + 10 * y + z } l.call 14, 9, 2 => 1492
l 19/12/11
Difference
Blocks vs Proc and Lambda1. blocks are not objects
2. blocks can not be assigned to a variable
Proc vs Lambda1. handling of parameters
2. returning from a method
l 19/12/11
Arrays
One array can contain any type of objects Grows automatically
l 1) with the new class method:
nums = Array.new
nums = Array.new(10) { |e| e = e * 2 }puts "#{nums}"
l 2) There is another method of Array, [].
nums = Array.[](1, 2, 3, 4,5)ORnums = Array[1, 2, 3, 4,5]
l 19/12/11
Hashes
l A Hash is a collection of key-value pairs like this: "employee" => "salary". It is similar to an Array, except that indexing is done via arbitrary keys of any object type, not an integer index.
l As with arrays, there is a variety of ways to create hashes. You can create an empty hash with the new class method:
l months = Hash.new
l Example:• num = Hash["a" => 100, "b" => 200]• keys = num.keys• puts keys
l 19/12/11
Symbols
l Symbol objects represent names and some strings inside the Ruby interpreter.A symbol is defined using a colon ":" in the beginning.
l Example:• :my_test_symbol
l A symbol is not a string, but it has a string representation and an object identifier.
l Immutable
Questions ?
Thank You