Chapter 6. Block Return Values: How Should I Handle This?

image with no caption

You’ve seen only a fraction of the power of blocks. Up until now, the methods have just been handing data off to a block, and expecting the block to handle everything. But a block can also return data to the method. This feature lets the method get directions from the block, allowing it to do more of the work.

In this chapter, we’ll show you some methods that will let you take a big, complicated collection, and use block return values to cut it down to size.

A big collection of words to search through

Word got out on the great work you did on the invoicing program, and your next client has already come in—a movie studio. They release a lot of films each year, and the task of making commercials for all of them is enormous. They want you to write a program that will go through the text of movie reviews, find adjectives that describe a given movie, and generate a collage of those adjectives:

The critics agree, Hindenburg is:

  • “Romantic”

  • “Thrilling”

  • “Explosive”

image with no caption

They’ve given you a sample text file to work off of, and they want you to see if you can make a collage for their new release, Truncated.

Looking at the file, though, you can see your work is cut out for you:

It’s true, this job is a bit complex. But don’t worry: ...

Get Head First Ruby 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.