0

I am new to ruby, and while learning it I didn't understand the concept of lambdas or procs. I know lambda and proc are the blocks of code that have been bound to a set of local variables. But I don't understand their use.

So

what advantage does a programmer get from it?

I had asked this question in past and got marked as duplicate and was given a link that had totally unrelated answered so please before marking it as duplicate or scolding me please view the answers of other links by yourself first.

Paul Richter
  • 10,908
  • 10
  • 52
  • 85
  • The problem is there are **many** posts on SO explaining what procs and lambdas are and how they work, so you will likely experience the same issue here. The question *what advantage does a programmer get from it?* is hard to answer, given its wording, but may be answered by something like [this answer](http://stackoverflow.com/a/7106640/877472), or [this one](http://stackoverflow.com/a/723/877472). – Paul Richter Nov 11 '14 at 20:25
  • 2
    Could you point out some of the threads that you believe people have incorrectly pointed out in the past? Regarding advantages, I've seen some code that passes around lambdas as variables to call at the last moment, sort of like a strange form of lazy evaluation (though in reality it's not lazy evaluation) – BookOfGreg Nov 11 '14 at 20:25
  • @BookOfGreg this is a thread http://stackoverflow.com/questions/11581308/what-are-procs-and-lambdas-practical-examples-please that i belive had irrelevant answer sinsce it only points out the difference which is not my concern right now – Haris Khan Laghari Nov 11 '14 at 20:35
  • Your question is too broad, and has been answered multiple times. If you feel that previous answers don't cover the specific things you want to know about, then it's up to you to ask the very specific questions that make it apparent how your question is different. – the Tin Man Nov 11 '14 at 20:37
  • @the Tin Man i mentioned in my question that what advantage does a programmer get from using it? – Haris Khan Laghari Nov 11 '14 at 20:49
  • Every programmer gains different advantages. You're asking for us to theorize over the advantages others have gained, which is too broad. Instead, I'd recommend searching to read the various blogs and articles about both, try them, and decide whether they are of advantage to you. In particular, consider the first two comments to the duplicated question. – the Tin Man Nov 11 '14 at 21:49
  • here's a good explanation on blocks, lambda and proc..give it a read => [blocks_lambda_proc](http://awaxman11.github.io/blog/2013/08/05/what-is-the-difference-between-a-block/) – sa77 Nov 12 '14 at 05:09

1 Answers1

0

This is a broad question. You're basically asking "why are closures important?". Two uses that come to mind for me are:

DISCLAIMER: I haven't actually run any of this code, but it should get the point across.

  1. Delayed execution of code. If I want to pass some code as a callback (e.g. Rails' after_create), I can use a closure to "hook" into Rails by passing a block. That block has the context of the current class, but it doesn't need to get run until Rails says so.

    class SuperClass
      def self.after_create(&block)
        @__after_create = block
      end
    
      def self.create
        # do normal create logic
        instance = self.new
        if @__after_create
          @__after_create.call(instance)
        end
      end
    end
    
    class MyClass < SuperClass
      after_create {|instance| instance.log}
    
      def log
        puts 'hello world!'
      end
    end
    
    MyClass.create
    
  2. Passing functions as parameters. This makes it easier to do things like write a generic tree traversal algorithm that just passes each node of the tree to some function:

    def Tree.elements
      ["hello", "world!"]
    end
    
    def Tree.traverse(&block)
      elements.each {|el| block.call(el)}
    end
    
    Tree.traverse {|el| puts el} # "hello" "world!"
    Tree.traverse {|el| puts el.size} # "5" "6"
    
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Josh Bodah
  • 1,223
  • 6
  • 17