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.

7
  • 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, or this one. Commented Nov 11, 2014 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) Commented Nov 11, 2014 at 20:25
  • @BookOfGreg this is a thread stackoverflow.com/questions/11581308/… that i belive had irrelevant answer sinsce it only points out the difference which is not my concern right now Commented Nov 11, 2014 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. Commented Nov 11, 2014 at 20:37
  • @the Tin Man i mentioned in my question that what advantage does a programmer get from using it? Commented Nov 11, 2014 at 20:49

1 Answer 1

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"
    
Sign up to request clarification or add additional context in comments.

1 Comment

def Tree.elements isn't terminated with end.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.