I understand the working of block , proc and lambda experession but what i dont understand that when to use anonymous method in code.
What is the problem which Anonymous solves ?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
One of the reasons I use lambdas in this way in Ruby is when I need a closure to capture stuff from the surrounding scope, which for example in smaller scripts I sometimes find more convenient than passing things around as arguments. Other people (ab)use top-level instance variables for that, but I don’t like that much.
Update as requested: Here’s a little example:
This is for a little tool I wrote for shifting subtitles. It’s a short script and writing it this way allowed me to close over
optionsand access it inside the lambda without having to pass it in. As I said, nothing funky, but for small scripts I sometimes like to do this (where “this” is parsing options, writing 1 or 2 lambdas that use those options, use the lambdas instead of methods later on).