Functional and Imperative Styles

On a few occasions we've used method names that end with an exclamation point. These are destructive methods, so called because when one is invoked on an object, the previous state of the object is gone forever. The nondestructive methods we've been using most of the time are sometimes called functional methods. To use one or the other sort of method consistently tends to categorize you into a programming style.

Someone who writes in the imperative style likes to use destructive methods, operating on objects “in place” as follows:

str = "A string\n"
str.gsub!(/[m-z]/,"-")
str.chomp!
str.swapcase!
str             #->  "a --I-G"

The functional programming style avoids destructive methods. Objects are not transformed directly. ...

Get Sams Teach Yourself Ruby in 21 Days now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.