Chapter 16. Ruby and functional programming

 

This chapter covers

  • A description of the functional style of programming
  • Pure functions
  • Method chaining and Kernel#itself
  • Higher-order functions
  • Method#curry, Proc#curry, and partial function application
  • Recursion

By now you know that Ruby is a powerful and expressive language that offers solutions to myriad software-programming challenges. Ruby encourages an object-oriented programming approach to language design, and thus far we’ve mostly restricted our study of programming principles to this discipline. But Ruby has long supported many elements of a functional programming style. In fact, in recent years Ruby has added even more language features to support writing code in a functional style.

The word “style” is important here. Ruby doesn’t meet the definition of a purely functional language because it doesn’t guarantee referential transparency or immutability (both defined in this chapter). Nevertheless, tenets of functional programming are available to the Rubyist. Software engineers can evaluate the pros and cons of each approach when choosing between object-oriented and functional programming styles. In a large enough program, mixing the two styles of programming is possible. As an engineer, you’ll find yourself frequently evaluating the trade-offs of one style over another.

16.1. Understanding pure functions

16.2. Immutability

16.3. Higher-order functions

16.4. Recursion

Summary

sitemap