If you have been in the Ruby community for any length of time, you have likely heard about Rack, probably along the lines of something being Rack-based, implements Rack or Rack middleware. Hopefully this post will help you understand exactly what everyone is talking about.

Rack describes itself as:

a minimal interface between webservers supporting Ruby and Ruby frameworks

While the concise explanation is helpful, it may still leave newcomers to programming slightly confused about what that actually means.

A minimal interface is nothing more than a component that is common ground and sits between two other components that need to work together. …

Delegation in programming is simply a matter of passing responsibility to an object more suitable to handle a request. It’s good to keep things simple and delegate when you can, that’s why Ruby has made doing so easy by providing the Forwardable module.

Including the Forwardable module in our classes gives us the ability to forward a request/method call to an underlying attribute more appropriate to deal with it.

Forwardable in the wild

Let’s assume that we’re building an app for an agency with agents who have different levels of clearance.

# agent.rb
class Agent
def initialize(name, level)
@name = name
@clearance_level = level…

Ruby has a nice shorthand syntax for methods that use blocks and accept Procs, which is very useful when working with enumerables e.g. each, map, reduce etc.

If you’ve been unaware of this syntax, to sum up the numbers in an array you’ve probably been writing code that looks like this:

[1,2,3,4].reduce do |total, number|
total + number
=> 10
[1,2,3,4].reduce { |total, number| total + number }
=> 10

The shorthand way of summing up these numbers can be done using the following:

=> 10
[1,2,3,4].reduce(100, &:+)
=> 110

As a personal preference I find this shorthand method much cleaner and easier to read. Another example of how we can use this with our own objects would…

Chris Temple

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store