Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Block, Proc and Lambda

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 409
    Comment on it

    Blocks, Procs, and Lambdas


    Hi friends,
    Today I am going to explain one of the most confusing part in Ruby, that is Block, Procs and Lambdas. It always troubles you during interviews or any other places wherever you deal with Ruby. But actually it shouldn't be confusing as you must have already used it. They are commonly used as inputs to some iterators like each and map.
    In brief, blocks are simply few lines of codes that are used together as an input to another method. They are also known as "anonymous functions", because they have no name but behave much like functions. You can declare a block by either using { } or do ... end. As an example:

     

    	> ["a","b","c"].each { |letter| print "#{letter.upcase} " }
    	A B C  => ["a", "b", "c"]
    	> ["a","b","c"].each do |letter|
    	>    print "#{letter.upcase}"
    	> end
    	A B C  => ["a", "b", "c"]
    


    Now lets talk about Procs, simply Proc is just a block, that you can save to a variable. In other words actually a block is a Proc (class name for a block), but blocks can be used separately also. So if you want to store a set of statements that can be used multiple times, you can use Proc.

    	> proc_obj = Proc.new { |argument| print "#{argument} " }
    	#Now this proc_obj can be passed to a loop like
    	> ["a","b","c"].each(&proc_obj)
    
    	#You can also directly call proc
    	> proc_obj.call("hello")
    


    Thus as we heard about Blocks and Procs, lets have a look at lambda. If you say Proc is a refined form of Block, then lambda is refinded form of Block. Lets know about the differences between procs and lambda

    • You can use explicitly return statement inside lambda as in lambda it will only return from the lambda itself that is not the case in proc as if you use return it will return from the block and itself too and will go back to the calling method.
    • The number of arguments can be passed to lambda are strict, it will throw an error if you pass wrong number of arguments.
    	> lambda do |letter| 
    	>   puts letter
    	>   return letter        # you can do this in lambdas not Procs
    	> end.call("hello ")
    	hello => "hello "        # not nil because we gave it a return
    


    Some more examples of using procs and lambdas are:

    	#short version lambda
    	> lamb_short = ->(a, b) { a * b }
    	> puts lamb_short.call(2, 4)
    	# => 8
    
    	#long version lambda
    	> lamb_long = lambda { |a, b| a * b }
    	> puts lamb_long.call(2, 4)
    	# => 8
    
    	#short version Proc
    	> proc_short = proc { |a, b| a * b }
    	> puts proc_short.call(2, 4)
    	# => 8
    
    	#long version Proc
    	> proc_long = Proc.new { |a, b| a * b }
    	> puts proc_long.call(2, 4)
    	# => 8
    


    Hope you liked reading this blog. For more blogs like this Click here

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: