Understanding Computation by Tom Stuart

Understanding Computation by Tom Stuart

Author:Tom Stuart
Language: eng
Format: epub, pdf
Tags: COMPUTERS / Programming / General
ISBN: 9781449330095
Publisher: O’Reilly Media
Published: 2013-05-14T16:00:00+00:00


Working with Procs

Since we’re going to be building entire programs out of procs, let’s spend a minute looking at their properties before we dive into using them.

Note

For the moment, we’re still using full-featured Ruby to illustrate the general behavior of procs. We won’t impose the restrictions until we start writing code to tackle The Problem.

Plumbing

Procs are plumbing for moving values around programs. Consider what happens when we call a proc:

-> x { x + 2 }.call(1)

The value that’s provided as an argument to the call, in this case 1, flows into the parameter of the block, in this case x, and then flows out of the parameter to all the places where it’s used, so Ruby ends up evaluating 1 + 2. It’s the rest of the language that does the actual work; procs just connect parts of the program together and make values flow to where they’re needed.

This already doesn’t bode well for our experiment in minimal Ruby. If procs can only move values between the pieces of Ruby that actually do something with them, how are we ever going to be able to build useful programs out of procs alone? We’ll get to that once we’ve explored some other properties of procs.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.