Chapter 9Writing Your Own Methods

As we’ve seen, loops and iterators allow us to do the same thing (run the same code) over and over again. However, sometimes we want to do the same thing a number of times but from different places in the program. For example, let’s say we were writing a questionnaire program for a psychology student. From the psychology students I have known and the questionnaires they have given me, it would probably go something like this:

puts ​'Hello, and thank you for taking the time to'
puts ​'help me with this experiment. My experiment'
puts ​'has to do with the way people feel about'
puts ​'Mexican food. Just think about Mexican food'
puts ​'and try to answer every question honestly,'
puts ​'with either a "yes" or ...

Get Learn to Program, 2nd Edition 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.