Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
Ruby - yield; parallel routines

When you call a function / method in your program, you expect it to run to completion before returning control to you, passing back a result. For example, you might call a method to read a series of objects and store them into an array ... and then you could take that array of objects and, iterating through it, analyse each object in turn.

Dam WallSuch an approach works well if you've not got very many objects, but it's not clever if you have a lot. You're going to spend a lot of time - and fill up a lot of memory - storing the objects; perhaps it will lead to out-of-memory conditions, or "thrashing" of swap space as your application pages data on and off the disc. When you think about it, reading the data in is like feeding water into a reservoir, and if you keep doing it the dam holding the water overflows or bursts.

A better approach is to read in the information object by object, then manipulate them one at a time. Such code can be easily enough written, but the code's all going to be in a main program loop (or at least it will contain a series of calls at the application level to atomic routines that process one record at a time - a poor structure with things that should be internal to the data handlers being visible at the application level.

The solution offered in Ruby - and it's a neat one - it to use an iterator function / method - a method which yields a result as it runs, making a temporary return and carrying on when resumed. This is one aspect of what's sometimes referred to as parallelism.

I have added three examples to our web site in the last couple of days. A "shows you how" mechanism is [here]. There's an example that drip feeds data as if from a tap [here] (and that comparative analogy is a good one), and there's a final example [here] in which two routines are paralleled - the first feeding the data from the tap drip by drip, and the second drinking it one drop at a time.

The concept of iterators is also applied in Lua where they're known as coroutines, and in Python where they're generator functions.

Illustration - Public domain / Dam at Kaw Lake. [source]
(written 2010-10-01)

Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
R112 - Ruby - Parallelism
  [4680] Processing data line by line - iterator in Ruby with yield - (2016-05-19)
  [3156] Splitting data reading code from data processing code - Ruby - (2011-02-04)

Back to
Christmas 2010 - Well House Manor, Melksham, Hotel
Previous and next
Horse's mouth home
Forward to
Ruby - examples of regular expressions, inheritance and polymorphism
Some other Articles
Strong arm tactics in the bus industry? Poor result for the customer!
Travel, Transport and Tourism - A vision for Melksham, 2026
How to set up short and meaningfull alternative URLs
Ruby - examples of regular expressions, inheritance and polymorphism
Ruby - yield; parallel routines
Christmas 2010 - Well House Manor, Melksham, Hotel
What is a factory method and why use one? - Example in Ruby
Creating, extending, traversing and combining Ruby arrays
Why do I need brackets in Ruby ... or Perl, Python, C or Java
Formatting your output - options available in Ruby
4727 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95 at 50 posts per page

This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2017: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/2979_Rub ... tines.html • PAGE BUILT: Sat Jun 11 12:16:26 2016 • BUILD SYSTEM: WomanWithCat