O'Reilly logo

Sams Teach Yourself Ruby in 21 Days by Mark Slagell

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required