Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Ruby basics ||


Published on

Loops, blocks, array & hashes.

Published in: Education, Technology
  • Be the first to comment

  • Be the first to like this

Ruby basics ||

  1. 1. RUBY BASICS - II
  2. 2. Abstract of Session  Loops  Iterators & Blocks  Array  Hashes  Iteration over Array and Hashes  Symbols l19/12/11
  3. 3. 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
  4. 4. 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
  5. 5. 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
  6. 6. 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
  7. 7. 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
  8. 8. 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
  9. 9. 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
  10. 10. 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
  11. 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
  12. 12. 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
  13. 13. 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
  14. 14. 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
  15. 15. 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
  16. 16. 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
  17. 17. Questions ?
  18. 18. Thank You