Skip to Main Content
Learn to Program, 2nd Edition
book

Learn to Program, 2nd Edition

by Chris Pine
March 2009
Intermediate to advanced content levelIntermediate to advanced
194 pages
4h
English
Pragmatic Bookshelf
Content preview from Learn to Program, 2nd Edition

Methods That Take Procs

When we pass a proc into a method, we can control how, if, or how many times we call the proc. For example, let’s say we want to do something before and after some code is run:

def​ do_self_importantly some_proc
puts ​"Everybody just HOLD ON! I'm doing something..."
some_proc.call
puts ​"OK everyone, I'm done. As you were."
end
say_hello = Proc.new ​do
puts ​'hello'
end
say_goodbye = Proc.new ​do
puts ​'goodbye'
end
do_self_importantly say_hello
do_self_importantly say_goodbye
Everybody just HOLD ON! I'm doing something...
hello
OK everyone, I'm done. As you were.
Everybody just HOLD ON! I'm doing something...
goodbye
OK everyone, I'm done. As you were.

Maybe that doesn’t appear particularly fabulous…but it is. It’s ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learn to Program, 3rd Edition

Learn to Program, 3rd Edition

Chris Pine
The Book of Ruby

The Book of Ruby

Huw Collingbourne

Publisher Resources

ISBN: 9781680500172Errata