Overview

16 Ruby and functional programming

```html

Ruby supports functional programming alongside its familiar object-oriented design, and this chapter frames that support as a matter of style rather than a strict identity. Ruby is not a purely functional language because it does not guarantee immutability or referential transparency, but it still offers many tools that let programmers write in a functional way when that style is a better fit. The chapter emphasizes that Rubyists often need to balance the strengths of both approaches, especially in larger programs where mixing styles can be practical and effective.

A central theme is the value of pure functions: operations whose results depend only on their inputs and that produce no side effects. The text contrasts such functions with methods that mutate variables, modify collections, print output, change database records, or raise exceptions unexpectedly. Built-in Ruby methods like arithmetic operators, map, and reduce illustrate the functional ideal because they return new values without changing their receivers, while bang methods and other mutating operations demonstrate how side effects arise in everyday Ruby code. The chapter also shows how reducing state, eliminating instance variables when possible, and using immutable or frozen objects can make code safer and easier to reason about.

The latter part of the chapter explores higher-order functions, method chaining, currying, pattern matching, and recursion as tools that strengthen Ruby’s functional style. Ruby methods can accept blocks or return procs, enabling reusable transformations and concise abstractions; itself and then help smooth method chains and support cleaner composition. Currying and partial application make it easier to build generic functions that can be specialized later, while recursion provides an alternative to iterative loops, especially when paired with lazy evaluation for large or infinite sequences. The chapter closes by noting that tail-call optimization can make recursive functions more practical, reinforcing the broader idea that functional techniques can be powerful additions to a Rubyist’s toolkit.

```

Summary

  • Functional and object-oriented programming styles differ in their approach to state, behavior, and program structure.
  • Pure functions and referential transparency are tenets of functional programming that contribute to terse, predictable code.
  • freeze and frozen? support immutable code and help prevent unintended side effects.
  • Currying and partial function application techniques enable Rubyists to create resusable functions.
  • The lazy method and tail-call optimization support efficient, memory-conscious functional programming.

FAQ

What is the functional programming style in Ruby?Ruby supports many functional-programming ideas, but it is not a purely functional language. In Ruby, the functional style emphasizes pure functions, immutability, higher-order functions, recursion, and minimizing side effects.
What is a pure function?A pure function returns the same result when given the same arguments and has no observable side effects. Its output depends only on its inputs.
What is referential transparency?Referential transparency means an expression can be replaced by its resulting value without changing the behavior of the program. For example, 3 + 4 can always be replaced with 7.
What are side effects in Ruby?Side effects are anything a method does beyond returning a value, such as mutating an object, printing to the console, changing a database record, or raising an unhandled exception.
How does Enumerable#map illustrate functional programming?map takes a block, transforms each element, and returns a new array without modifying the receiver. That makes it a good example of a pure, higher-order function.
What is immutability in Ruby, and how do freeze and frozen? help?Immutable objects do not change after creation. In Ruby, freeze prevents further modification, and frozen? tells you whether an object is frozen.
What is Kernel#itself used for?itself simply returns the object it is called on. It is often used to simplify method chains, such as with group_by(&:itself).
What is a higher-order function?A higher-order function takes a function as an argument, returns a function, or both. In Ruby, methods that accept blocks or return procs can be higher-order functions.
What is currying in Ruby?Currying turns a function into a sequence of functions that each take fewer arguments, often one at a time. Ruby’s curry method can also support partial function application.
Why is recursion useful in functional programming, and what is a terminal clause?Recursion lets a function call itself to solve a problem in smaller steps, often without side effects. A terminal clause is the condition that stops the recursion and returns a final value.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • The Well-Grounded Rubyist, Fourth Edition ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • The Well-Grounded Rubyist, Fourth Edition ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • The Well-Grounded Rubyist, Fourth Edition ebook for free