From: John Morrice Date: 2010-11-18T21:08:59+09:00 Subject: Re: Ruby Not observing DRY principle > > I don't really understand what you're trying to do there, but I > > think you need: > > > > - An event handler (to intercept user input) > > - Blocks to handle each of your events > > - The process itself (a class representing what the program does) > > ** And, if the user is calling algorithms, then each algorithm > > belongs to it's own class/function.  How you call them is a matter > > of taste (I would push a hash with algorithm parameters). That is an example of the strategy pattern! http://www.c2.com/cgi/wiki?StrategyPattern > option1 > foo(set of variables, user input 1) > # set of variables will be different for each option > # Sets will have same variable types but different values > option 2 > foo(set of variables, user input 1) > foo(set of variables, user input 2) > option 3 > foo(set of variables, user input 1) > foo(set of variables, user input 2) > option 4 > foo(set of variables, user input 1) > foo(set of variables, user input 2) Is this sort of thing what you mean? # The main loop def main loop do puts "Please choose an option:" puts " [1] Option1" puts " [2] Option2" puts " [3] Option3" opt = nil # A primitive event handler case gets.to_i when 1 opt = Option1.new when 2 opt = Option2.new when 3 opt = Option3.new end # Check if a valid option was entered if opt opt.run else puts "Error: please enter valid option." end end end # Run the program main You also need to define # The first option. I wish I had a better name! class Option1 # get some details # send to file dated with date user supplied # send to database def run end end Flebber, the benefit of using objects to represent your options, rather than just functions, is that you may then apply the various object oriented techniques. E.g. TemplateMethod could remove duplicate code from similar Options http://www.c2.com/cgi/wiki?TemplateMethod However, using a strict object oriented approach can be pretty heavy-weight! Johnny