Blocks and loops.pptx
-
Upload
sandeep-kumar -
Category
Technology
-
view
265 -
download
0
Transcript of Blocks and loops.pptx
RUBY BLOCKS, LOOPS/ITERATORS & FLOW CONTROLS
● Ruby blocks● Ruby yield● Block, proc and lambda● Iterators: ruby-loops● Flow controls
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.
#example1 def test
puts “we are in method”yieldputs “we are in method”yield
EndTest { puts “we are in block”}>>> result will be :You are in the method You are in the block You are again back to the method You are in the block #example210.times {|a| puts a }
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.
How Proc works?class Array def iterate!(code) self.each_with_index do |n, i|
self[i] = code.call(n) end end end array_1 = [1, 2, 3, 4] array_2 = [2, 3, 4, 5] square = Proc.new do |n|
n ** 2 end array_1.iterate!(square) array_2.iterate!(square)
#example2def callbacks(procs) procs[:starting].call
puts "Still going" procs[:finishing].call end callbacks(:starting => Proc.new { puts "Starting" }, :finishing => Proc.new { puts "Finishing" })
Lambda: an example, how its different from Proc:#example1def args(code)
one, two = 1, 2 code.call(one, two)
end args(Proc.new{|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
‘method’ ruby method: class Array def iterate!(code) self.each_with_index do |n, i|
self[i] = code.call(n) end end end def square(n) n ** 2 end array = [1, 2, 3, 4] array.iterate!(method(:square))
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 block1puts "Start of block1..."yieldyieldputs "End of block1."
endblock1 {puts "yielding...”}
○ we can wrap yield with if statement as well likeyield if block_given?
○ block_given? What is this?
Ruby Iterators: looping
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.
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.
#example1-looploop {puts "HELLO"}i=0 loop do
i+=1 print "#{i} " break if i==10 end
#example2-whilei=1 while i < 11
puts i i+=1
end
#example3-untili=1 until I > 10
puts i i+=1
end
#example4-while/until modifiersi=0 puts I while i < 10 i=0Puts i until I == 10
#example5-fori=1 for i in 1..10
puts i end
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.
‘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 they handle the scoping of new variables differently: each makes them disappear outside the block, whereas for makes them persist. With experience you’re likely to find you want new variables to be confined to the block, making each the better choice.
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
QUESTIONS???