| |||
yield and iterator - practical example
Parallelism example from a Well House Consultants training course
More on Parallelism [link]
Source code: feeder Module: R112
# yield and iterator - practical example
# An iterator that generates data one line at a time def stepthrough(source) fh = File.new source fh.each_line do |record| if record =~ /option247/ yield record puts $. end end end # An action routine that handles each line def report(line) fol = line.chomp.split(" ") puts "#{fol[1]} #{fol[6]} #{fol[8]}" end # The glue that holds them together c = 0 stepthrough("ac_20160516") { |item| report item c += 1 } puts "#{c} matches" puts $. # Sample output __END__ WomanWithCat:4c grahamellis$ ruby feeder option247.uk /bususer_wilts_20160404.pdf 200 2531 option247.uk /robots.txt 200 4565 option247.uk /O247images/facebook.png 200 4568 option247.uk /O247images/3.jpg 200 5392 option247.uk /O247images/4.jpg 200 5719 option247.uk /O247images/woman4.jpg 200 6030 option247.uk /O247images/2.jpg 200 6766 option247.uk /O247images/O247sidebar.jpg 200 7076 option247.uk /O247images/twitter.png 200 7246 option247.uk /O247images/man3.jpg 200 8135 option247.uk /css/multiColumnTemplate.css 200 8243 option247.uk /already.html 200 8251 option247.uk /robots.txt 200 8271 option247.uk /option6.pdf 200 8275 option247.uk /robots.txt 200 8692 option247.uk /O247images/bbcwiltshire.jpeg 200 8920 option247.uk /robots.txt 200 8939 option247.uk /standby.html 200 8940 option247.uk /O247images/woman1.jpg 200 8980 option247.uk /robots.txt 200 9002 option247.uk / 200 9004 option247.uk /O247images/1.jpg 200 9114 option247.uk /faq.html 200 9435 option247.uk /faqextra.html 200 9436 option247.uk /o247_20160407.pdf 200 9452 option247.uk /designatedbuses.pdf 200 9453 option247.uk /outline.html 200 9457 option247.uk / 200 9459 option247.uk /management.html 200 9460 option247.uk /247presentation_20160414.pdf 200 9461 option247.uk /bususer_wilts_20160404.pdf 200 9462 option247.uk /robots.txt 200 9513 option247.uk /techschemedraft.pdf 304 9514 option247.uk /O247images/melkshamnews.png 200 9636 option247.uk /O247images/man2.jpg 200 12537 option247.uk /favicon.ico 200 12814 option247.uk /O247images/email.png 200 13849 option247.uk / 200 14012 option247.uk / 200 14369 option247.uk /apple-touch-icon-precomposed.png 404 14370 option247.uk /apple-touch-icon.png 404 14372 option247.uk / 200 14373 option247.uk /apple-touch-icon-precomposed.png 404 14374 option247.uk /apple-touch-icon.png 404 14375 option247.uk /robots.txt 200 14889 option247.uk /robots.txt 200 14890 option247.uk /robots.txt 200 14892 option247.uk / 200 14893 option247.uk /hopalong.pdf 200 14895 option247.uk /people.html 200 14896 option247.uk /faq.txt 200 14897 option247.uk /robots.txt 200 15408 option247.uk / 200 15409 option247.uk /robots.txt 200 15681 option247.uk /management.html 200 15683 option247.uk / 200 17122 option247.uk /option2.pdf 200 18843 option247.uk /video.html 200 19018 option247.uk /links.html 200 19019 option247.uk /faqindex.html 200 19021 option247.uk /rg_twsw_20160121.pdf 200 19377 option247.uk /local-bus-market-study-access.pdf 200 20205 option247.uk / 200 20745 option247.uk /faqextra.html 200 20746 option247.uk /css/multiColumnTemplate.css 200 22368 option247.uk / 200 23234 option247.uk /css/multiColumnTemplate.css 200 23235 option247.uk /O247images/O247sidebar.jpg 200 23237 option247.uk /O247images/facebook.png 200 23238 option247.uk /O247images/email.png 200 23239 option247.uk /O247images/bbcwiltshire.jpeg 200 23240 option247.uk /O247images/melkshamnews.png 200 23241 option247.uk /O247images/4.jpg 200 23250 option247.uk /O247images/woman1.jpg 200 23254 option247.uk /O247images/twitter.png 200 23255 option247.uk /O247images/1.jpg 200 23256 option247.uk /O247images/3.jpg 200 23257 option247.uk /O247images/man2.jpg 200 23260 option247.uk /O247images/2.jpg 200 23263 option247.uk /O247images/man3.jpg 200 23264 option247.uk /O247images/woman4.jpg 200 23267 option247.uk /favicon.ico 200 23277 option247.uk /option1.pdf 200 23415 option247.uk /option1.pdf 200 23420 option247.uk /option3.pdf 200 23524 option247.uk /option3.pdf 200 23545 option247.uk /o247_2.pdf 200 23597 option247.uk /o247_2.pdf 200 23693 option247.uk / 200 23785 option247.uk /apple-touch-icon-precomposed.png 404 23786 option247.uk /apple-touch-icon.png 404 23787 option247.uk / 200 23788 option247.uk /apple-touch-icon-precomposed.png 404 23789 option247.uk /apple-touch-icon.png 404 23790 option247.uk /subsidised_summer_2015.pdf 200 25283 option247.uk /commercialbusmap.jpg 200 26045 option247.uk /rarebusmap.pdf 200 28595 option247.uk /robots.txt 200 28620 option247.uk /outline.html 200 28622 option247.uk /people.html 200 28623 option247.uk /faq.html 200 28837 option247.uk /O247images/twitter.png 200 28881 option247.uk /robots.txt 200 29447 option247.uk /outline.html 200 29448 option247.uk /robots.txt 200 30467 option247.uk /contact.html 200 30474 option247.uk /faq.html 200 30480 option247.uk /index.html 200 30487 option247.uk /links.html 200 30490 option247.uk / 200 31314 option247.uk /robots.txt 200 31501 option247.uk /contact.html 200 31502 option247.uk /robots.txt 200 31668 option247.uk /already.html 200 31669 option247.uk /Service_x72_web.pdf 200 32079 option247.uk /robots.txt 200 32795 option247.uk /cpm_mlm.pdf 304 32811 option247.uk /robots.txt 200 34602 option247.uk / 200 34692 option247.uk /Service_x72_web.pdf 200 39657 option247.uk /subsidised_summer_2015.pdf 200 41278 option247.uk /Service_x72_web.pdf 200 41304 option247.uk /favicon.ico 200 41307 option247.uk /index.html 200 41901 option247.uk / 200 42237 option247.uk /css/multiColumnTemplate.css 200 42238 option247.uk /O247images/melkshamnews.png 200 42239 option247.uk /O247images/email.png 200 42240 option247.uk /O247images/facebook.png 200 42241 option247.uk /O247images/bbcwiltshire.jpeg 200 42242 option247.uk /O247images/O247sidebar.jpg 200 42243 option247.uk /O247images/1.jpg 200 42244 option247.uk /O247images/4.jpg 200 42245 option247.uk /O247images/twitter.png 200 42246 option247.uk /O247images/2.jpg 200 42247 option247.uk /O247images/woman1.jpg 200 42248 option247.uk /O247images/3.jpg 200 42249 option247.uk /O247images/man2.jpg 200 42250 option247.uk /O247images/man3.jpg 200 42251 option247.uk /O247images/woman4.jpg 200 42252 option247.uk /o247_2.jpg 200 43506 option247.uk /O247images/1.jpg 200 43513 option247.uk / 200 45035 option247.uk /Service_x72_web.pdf 200 45049 option247.uk / 200 45261 option247.uk /css/multiColumnTemplate.css 200 45262 option247.uk /O247images/O247sidebar.jpg 200 45263 option247.uk /O247images/facebook.png 200 45264 option247.uk /O247images/bbcwiltshire.jpeg 200 45265 option247.uk /O247images/email.png 200 45266 option247.uk /O247images/melkshamnews.png 200 45267 option247.uk /O247images/man2.jpg 200 45268 option247.uk /O247images/3.jpg 200 45269 option247.uk /O247images/1.jpg 200 45270 option247.uk /O247images/4.jpg 200 45271 option247.uk /O247images/woman4.jpg 200 45272 option247.uk /O247images/man3.jpg 200 45273 option247.uk /O247images/2.jpg 200 45274 option247.uk /O247images/woman1.jpg 200 45275 option247.uk /O247images/twitter.png 200 45276 option247.uk /O247images/melkshamnews.png 200 45574 option247.uk /Service_x72_web.pdf 200 49969 162 matches 49999 WomanWithCat:4c grahamellis$ Learn about this subject
This module and example are covered on the following public courses:
* Learning to program in Ruby * Ruby Programming Also available on on site courses for larger groups Books covering this topic
Yes. We have over 700 books in our library. Books
covering Ruby are listed here and when you've selected a
relevant book we'll link you on to Amazon to order.
Other Examples
This example comes from our "Parallelism" training module. You'll find a description of the topic and some
other closely related examples on the "Parallelism" module index page.
Full description of the source code
You can learn more about this example on the training courses listed on this page,
on which you'll be given a full set of training notes.
Many other training modules are available for download (for limited use) from our download centre under an Open Training Notes License. Other resources
• Our Solutions centre provides a number of longer technical articles.
• Our Opentalk forum archive provides a question and answer centre. • The Horse's mouth provides a daily tip or thought. • Further resources are available via the resources centre. • All of these resources can be searched through through our search engine • And there's a global index here. Purpose of this website
This is a sample program, class demonstration or answer from a
training course. It's main purpose
is to provide an after-course service to customers who have attended our
public private or
on site courses, but the examples are made
generally available under conditions described below.
Web site author
Conditions of use
Past attendees on our training courses are welcome to use individual
examples in the course of their programming, but must check
the examples they use to ensure that they are suitable for their
job. Remember that some of our examples show you how not to do
things - check in your notes. Well House Consultants take no responsibility
for the suitability of these example programs to customer's needs.
This program is copyright Well House Consultants Ltd. You are forbidden from using it for running your own training courses without our prior written permission. See our page on courseware provision for more details. Any of our images within this code may NOT be reused on a public URL without our prior permission. For Bona Fide personal use, we will often grant you permission provided that you provide a link back. Commercial use on a website will incur a license fee for each image used - details on request. | |||
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho PAGE: http://www.wellho.net/resources/ex.php • PAGE BUILT: Sun Oct 11 14:50:09 2020 • BUILD SYSTEM: JelliaJamb |