DEV Community 👩‍💻👨‍💻

subhashree
subhashree

Posted on

Ruby Lambda when and how ??????

Most of us would have seen 'n' number of posts on how to use lambda( I have seen at least 10+ posts), but eventually will end up writing common methods. I have never used block call or yield, as it looks like a complicated feature that my brain never wants to learn it( "why to complicate things ! :D").

Unfortunately, I have to use it in my work, to avoid duplicate issues. Then I told myself, if not now then when?

Before jumping into the code, we should be aware of what is lambda and its syntax.

Alt Text


Source: https://www.rubyguides.com/2016/02/ruby-procs-and-lambdas/

Note: '->' is equivalent to lambda literal

Now you have seen that condition alone changes but calculating steps remain the same.

How these repeating steps can be moved to a common method?

We can pass FACTOR and THRESHOLD, but how about operators?

Is there a way that I can pass this condition alone from the calling method and make it simple?

To answer all the above questions, we have our LAMBDA for it.

Now repeating steps are moved into a common method called process_score, having score and lambda as arguments.

We can do this in different ways, but this post is exclusively for how we can make use of lambda.

Thanks for reading! Suggestions are welcome!
Note: This is post originally posted in my medium account https://medium.com/@subha220993/oh-my-lambda-a1386a0bdad2

Top comments (0)

🌚 Friends don't let friends browse without dark mode.

Sorry, it's true.