From: Robert Klemme Date: 2010-04-16T20:16:55+09:00 Subject: Re: Is this good OOP structuring? 2010/4/16 Derek Cannon : > Robert, I think you were meaning to thank Josh! Oh, yes of course. Thanks for the correction! > Anyway, thanks very much > Josh for the great explanations. That answered all my questions! And > Robert, your example really helped -- it was a great concrete example. Good. > I guess my only question now is: when is it appropriate to make methods > static? In the example that was given earlier: > >>class CourseController >>   def initialize(...) # whatever it needs >>   end >> >>   def self.read_url(url) >>     ... >>     cc = new(...) >>     ... >>     cc >>   end >> >>   def self.read_file(file_name) >>   end >>end > > I understand how one would use CourseController.read_url(xxx) and > CourseController.read_file(xxx), but I don't understand why I'd need it. > > Without it being static, I could just make references to the instance > variables, in this case, the url -- which would be passed through in the > initialize constructor). That would eliminate the need for the parameter > that both static methods carry, right? No. The static methods would just be convenience methods for frequent ways to construct the CourseController. Example require 'open-uri' class CourseController def initialize(courses_string) # parse string and create internal structure end def self.read_file(file_name) new(File.read(file_name)) end def self.read_url(url) open(url) {|io| new(io.read)} end end You can even modularize it more by adding a method that will do the parsing and output a complete initialized CourseController. #initialize then might have an empty argument list or a different list. Of course I am speculating a bit here since I do not know your application case's details. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/