Ruby Lamdas For Clear Code

date: "2024-11-18"
Categories: ["Development", "ruby", "rubylang"]
Tags: ["Development", "Ruby", "Functional"]

Naming block code

Ruby lambdas can be used for documenting code by adding a name to an algorithm.

users.map { |u| u.first_name = " " + u.last_name }

You can use a lambda to make it a bit more clear.

full_name = -> a { a.first_name + a.last_name }
users.map(&full_name)

In this case & calls the to_proc method on lambda.

Here’s another example:

users.select { |a| a.age > 18 }

It can be refactored to

adult = -> a { a.age > 18 }
users.select(&adult)

You can then reuse this adult lambda in combination with other lambdas.

male = -> a { a.gender == :male }
users.select { |a| adult.(a) && male.(a) }

Aliasing method names

Lambda can also be used for aliasing a method with a name more close to the current context.

If we look at this code:

key = Digest::MD5.hexdigest(Digest::MD5.hexdigest(login) + Digest::MD5.hexdigest(user.keyword))

It can be hard to read, but lets extract the md5 calculation in a lambda:

md5 = -> str { Digest::MD5.hexdigest(str) }
key = md5.(md5.(login) + md5.(user.keyword))

This makes the code clearer and more concise. The signal to noise ratio is much higher this time.

Another way of doing this would be to create a method on the current class. However this is more code and the md5 method definition would be far from its call location even if the method would be use only on the caller method.

Using lambdas for creating very simple DSLs

When we say simple we don’t mean less powerful.