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.

Blocks and loops.pptx


Published on

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

  • Be the first to like this

Blocks and loops.pptx

  2. 2. ● Ruby blocks● Ruby yield● Block, proc and lambda● Iterators: ruby-loops● Flow controls
  3. 3. Ruby blocks:○ Block consists of a chunk of code○ We assign a name to block○ Code inside a block is always enclosed within {} braces or do- end.○ A block can always be invoked from a function with same name as the block. E.g. a block has name ‘check’ then it can be invoked from a method named ‘check’.○ We can invoke a block with using ‘yield’ statement.○ We can pass parameters to block as well.○ Real-time examples of block are array collect, select, map within which can simply attach a block of code and process that code on array, times loop i.e. 5.times do |a| …end.
  4. 4. #example1def test puts “we are in method” yield puts “we are in method” yieldEndTest { puts “we are in block”}>>> result will be :You are in the methodYou are in the blockYou are again back to the methodYou are in the block#example210.times {|a| puts a }
  5. 5. Block, Proc, Lambda & methods:○ Blocks, procs and lamdba are one of the most powerful aspects of ruby, and also one of the most misunderstood too.○ This can be because ruby handles CLOSURES in a bit different/unique way. To make it more difficult its has four different ways of handling closures.○ Block: most common, easiest and arguably most “ruby like” way to use closures in ruby.○ Procedures AKA Procs: blocks are good, but we cant reuse them, whenever we need we have to rewrite them. There comes Procs. Procs are nothing more than a block that can be reused.○ Lambda: lambda is almost similar to Procs except two subtle differences, ○ unlike Procs, lambdas check the number of arguments passed and returns error if not. ○ lambdas have diminutive returns.○ Method: when we already have some method and want to pass it to another method as a closure, we use ‘method’ method provided by ruby to keep things DRY.
  6. 6. How Proc works?class Array def iterate!(code) self.each_with_index do |n, i| self[i] = end endendarray_1 = [1, 2, 3, 4]array_2 = [2, 3, 4, 5] #example2 def callbacks(procs) procs[:square = do |n| starting].call n ** 2 puts "Still going" procs[:end finishing].callarray_1.iterate!(square) endarray_2.iterate!(square) callbacks(:starting => { puts "Starting" }, :finishing => { puts "Finishing" })
  7. 7. Lambda: an example, how its different from Proc:#example1def args(code) one, two = 1, 2, two)endargs({|a, b, c| puts "Give me a #{a} and a #{b} and a #{c.class}”})args(lambda{|a, b, c| puts "Give me a #{a} and a #{b} and a #{c.class}”})#example2def lambda_return lambda { return "lambda" }.call return "lambda_return method finished"Endputs lambda_return#=> lambda_return method finished
  8. 8. ‘method’ ruby method:class Array def iterate!(code) self.each_with_index do |n, i| self[i] = end endenddef square(n) n ** 2endarray = [1, 2, 3, 4]array.iterate!(method(:square))
  9. 9. Yield???○ Placeholder for any code-block i.e. block, proc or lambda.○ Yield lets us branch from within a method and execute some external code, then return to the original code in the first method.○ Blind date?? def block1 puts "Start of block1..." yield yield puts "End of block1." end block1 {puts "yielding...”}○ we can wrap yield with if statement as well like yield if block_given?○ block_given? What is this?
  10. 10. Ruby Iterators: looping
  11. 11. Ruby Loops/Iterators:● Loops/Iterators in Ruby are used to execute the same block of code a specified number of times.● In ruby, for better understanding, we can assume to classify looping/iterating in 2 buckets: ● Simple way to loop/iterate. ● Complex way to loop/iterate.● Common Looping ways we use in ruby are for, while, loop, until, while/until modifiers etc.● Common iterating ways we use in ruby are each iterators, time iterators, upto & step iterators, each_index iterators.
  12. 12. Ruby Looping Constructs:○ Loop: The simplest looping constructs in ruby is ‘loop’ method. Technically it’s an iterating block as it takes a block as input.○ While: The while loop in Ruby is just like the standard while loop in any other language nothing too fancy.○ Until: The until loop is similar to the while loop but the logic is reversed.○ While/until modifiers: Ruby also allows you to use the while and until keywords as modifiers, which means you can put them at the end of an expression just like you can do with the if statement.○ For: If we discount the loop method then the for loop acts as a kind of bridge between looping constructs and iterators in Ruby. The for loop is still a looping construct but it acts almost like an iterator without actually taking a block.
  13. 13. #example1-looploop {puts "HELLO"} #example2-whilei=0 i=1loop do while i < 11 i+=1 print "#{i} " puts ibreak if i==10 i+=1end end #example3-until #example4-while/until modifiers i=1 i=0 until I > 10 puts I while i < 10 puts i i+=1 i=0 end Puts i until I == 10 #example5-for i=1 for i in 1..10 puts i end
  14. 14. Ruby Iterators:○ Iterators are methods that take blocks and execute that block as many times as there are iterations.○ Each iterators(something.each do ... end) ○ Iterates through each elements.○ Times iterators(x.times do … end) ○ The times iterator is similar to you classic for loop in other languages and will allow you to execute a loop and perform an action (according to the block you write) x number of time○ Upto and step iterators ○ This is also similar to for loop too, that we execute from number x up to number y. ○ Step iterators helps to skip few steps between.
  15. 15. ‘Each’ Iterators:array = [1,2,3,4,5,6,7,8,9,10]array.each {|value| print "#{value} "}(1..5).each { puts ‘We are here!’ }(1…5).each { puts ‘we are here’}****Also, for does not exactly qualify as a syntax sugar for each, because theyhandle the scoping of new variables differently: each makes themdisappear outside the block, whereas for makes them persist. Withexperience you’re likely to find you want new variables to be confined tothe block, making each the better choice.
  16. 16. Control Flows:○ Ruby offers conditional structures that are pretty common to modern languages.○ If statement.○ If-else statement.○ If modifier statement.○ Unless statement.○ Unless modifier.○ Case statement. case expr0 when expr1, expr2 stmt1 when expr3, expr4 stmt2 else stmt3 end
  17. 17. QUESTIONS???