From: flebber Date: 2010-11-19T06:05:25+09:00 Subject: Re: Ruby Not observing DRY principle On Nov 18, 11:45 pm, shadowfirebird wrote: > It appears that my mail client is having trouble -- hence the blank > message bodies.  Sorry about that. > > Flebber, if you have a simple application and aren't coding to learn > about OOP, it may be that you simply don't need classes.  In Ruby, > they are optional. > > You should think of classes as a way of wrapping functionality and > data together.  You've not told us anything about your data, or how > complex foo() is, so I'm not sure we can give you more concrete > suggestions.   But if you start thinking about how the data needs to > be organised, and what operations the data needs to be able to perform > on itself, then either some classes will "pop out" -- or they won't, > and you'll know that this program does not need to be OOP. > > With regard to your option selection block, I would personally put > that functionality into foo, and just pass foo a "mode" variable, so: > > loop do >     mode, data = getinput() >     break if (mode == "quit") > >     foo(mode, data) > end > > I'm not sure why your pseudocode asks for input twice and then runs > foo() twice, but presumably there is a good reason. > > Likewise, we don't know whether you plan to gather input using a GUI > (which would involve an event-driven approach) or at the command line, > for instance (which would allow much simpler code). > 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 Yes but this is how initially I started to write my code but then had the thought that since my calculations weren't computer intensive. So I thought why not calculate all options first and show what valid options exist to the user rather than looping around the options trying to find a valid option. Is this a good way to go. > I'm not sure why your pseudocode asks for input twice and then runs > foo() twice, but presumably there is a good reason. > Likewise, we don't know whether you plan to gather input using a GUI > (which would involve an event-driven approach) or at the command line, > for instance (which would allow much simpler code). I ask after the program has identified there is a valid option and the user wants to select it, it is valid data but only names and identifiers that will be recorded in a database not relevant to whether the calculation is valid. i will be using a command line solution. My calculations aren't complex they simply pass through a small formula and check whether a ratio resolves to true. If both true then its a valid option the user can choose.