Ruby basics ||
Upcoming SlideShare
Loading in...5

Ruby basics ||



Loops, blocks, array & hashes.

Loops, blocks, array & hashes.



Total Views
Slideshare-icon Views on SlideShare
Embed Views



0 Embeds 0

No embeds


Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

    Ruby basics || Ruby basics || Presentation Transcript

    • Abstract of Session  Loops  Iterators & Blocks  Array  Hashes  Iteration over Array and Hashes  Symbols l19/12/11
    • Loops ? Loops in Ruby are used to execute the same block of code a specified number of times until some condition is met. l19/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) 2) 3) while condition do code end code while condition begin code end while condition l19/12/11
    • Example What is difference ? In 2nd or 3rd case, code is executed once before conditional is evaluated. example : num = 0 max = 5 while num < max puts("Inside the loop #{num}") num += 1 end l19/12/11
    • until Executes code when condition is false. There are 3 ways to use until loop Syntax : 1) until condition do code end 2) 3) code until condition begin code end until condition l19/12/11
    • For Loop lexample : l •for num in 0..5 puts("Inside the loop #{num}") •end lO/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 l19/12/11
    • times lexample : l • 5.times do |index| puts("Inside the loop #{index}") •end lO/P : •Inside the loop 0 •Inside the loop 1 •Inside the loop 2 •Inside the loop 3 •Inside the loop 4 l19/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 l19/12/11
    • each lSyntax : •collection.each do |variable| l code •end lExample : l •num_array = [1,2,3,4,5] •num_array.each do |num| puts num •end lYou 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. l19/12/11
    • collect lThe collect iterator returns all the elements of a collection. lSyntax : •collection = collection.collect lThe 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. lexample : •num_array = [1,2,3,4,5] •new_array = num_array.collect{|num| 10*num} •puts new_array l19/12/11
    • What are blocks? lA block consists of chunks of code. lYou assign a name to a block. lThe code in the block is always enclosed within braces {}. lA 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. lYou invoke a block by using the yield statement. l19/12/11
    • Blocks lSyntax : •block_name{ l statement1 l statement2 l .......... •} lExample : •def test l puts "You are in the method" l yield l puts "You are again back to the method" l yield •end •test {puts "You are in the block"} l19/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 = { |x, y, z| puts 100 * x + 10 * y + z } 14, 9, 2 => 1492 l19/12/11
    • Arrays  One array can contain any type of objects  Grows automatically l1) with the new class method: nums = nums = { |e| e = e * 2 } puts "#{nums}" l2) There is another method of Array, []. nums = Array.[](1, 2, 3, 4,5) OR nums = Array[1, 2, 3, 4,5] l19/12/11
    • Hashes lA 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. lAs with arrays, there is a variety of ways to create hashes. You can create an empty hash with the new class method: lmonths = lExample: •num = Hash["a" => 100, "b" => 200] •keys = num.keys •puts keys l19/12/11
    • Questions ?
    • Thank You