Procs & Lambdas in Ruby

Functional Programming: First-class functions with Ruby

Procs & Lambdas in Ruby

You’ve probably read or seen a lot of Functional Programming (FP) articles recently. The more I read about FP, the more it grows on me. But I’m a Ruby on Rails developer. Is it even possible to adopt it in my code? Well, we can, by stealing one of the best parts of Functional Programming with the help of Procs & Lambdas.

Pure functions, eliminating side-effects, passing functions as parameters are the essence of Functional Programming. I love those principals but it goes against a lot of what makes Rails, Rails. Well, as it turns out in Ruby, we can implement “passing functions as parameters” today. In FP, this is known “First-class Functions”. 

So let’s dive straight into using Procs and Lambdas and their differences. They’re the only way to *pass the method itself as a parameter, rather than the resulting value* in Ruby.

Check this out:

Cool, eh? With one line of code, we defined a higher-order function called square. Assigned it a lambda expression that takes one parameter, x. When called, it will square x and return the result.

In mathematics and computer science, a higher-order function is a function that does at least one of the following: Takes one or more functions as arguments OR returns a function as its result

The best bit is where we call the function perform on line 11. We pass square (not its result) and the integer 2 into perform. This then invokes square with the parameter as which returns 4.

my_lambda.call() or my_lambda.call(my_param) allows us to invoke and/or pass in a value to the lambda. A lambda’s parameters are defined in it’s block arguments |my_param|.

Procs and Lambdas have *two* distinct differences

1. They Handle Parameters Differently

Lambdas

The first puts statement will print 3.

However the second puts statement will throw an error:

Lambdas are treated more like methods when it comes to parameter handling.

Procs

The first puts statement will print 1.

However the second puts statement will throw an error:

Procs are just blocks that treat their parameters as 'nil' if they aren’t provided.

2. They Handle *return* Differently

Lambdas

As we’ve seen so far these are treated more like methods in Ruby. If we call 'return' inside a lambda it will return directly to the method that called it.

The lambda we called returns to our method 'lambda_caller' to continue execution.

Procs

The Proc jumps outside of the calling method 'proc_caller' and returns “Hello Proc!” instead.

Thoughts?

Hopefully, this gives you another way to think about how to structure your methods in Ruby. Things like this can be a breath of fresh air for boring projects. Most of all, have fun writing code. I’m not saying, go nuts and refactor your entire codebase. Use it sparingly where it makes sense to do so.

If you want to learn more about Functional Programming  — I recommend this fantastic book: Functional Programming — A PragPub Anthology.

Whether or not you found this article useful, I’d love to hear from you. Get in touch.

!@THEqQUICKbBROWNfFXjJMPSvVLAZYDGgkyz&[%r{\"}mosx,4>6]|?'while(putc 3_0-~$.+=9/2^5;)<18*7and:`#

Need a Quote for a Project?

We’re ready to start the conversation however best suits you - on the phone at
+353 (0)1 833 7392 or by email