From: Anurag Priyam Date: 2011-02-23T23:09:48+09:00 Subject: Re: Calling class method from instance method > You see, there's stuff that need to be donne in both cases, in instances > and in classes. For example: > > class SomeClass >  def self.method >    if file_exists? >      #do stuff >    end >    #something need to be donne here, and I need to check if the file > exists >    #I dont want to define a nother class method exactly the same as the > already declared instance method. >  end Since file_exists? is an instance method, it is very likely to return true/false based on the state of the instance (see below). Calling file_exists in self.method does not make sense unless its responsibility is to act on an instance of your class - like a factory, or processing an instance passed to it as a parameter. def self.method(object) if object.file_exists? # do something on that object object.statistics end end or, def self.create(*args) object = SomeClass.new(*args) return object if object.file_exists? end >  def file_exists? >    #there are instance methods depending on this one >    File.exists?("some_file_name.txt") >  end Umm, is the file name hard coded, or does it depend on the state of the instance, like: def file_exists? File.exists?(@file) end If it is hard coded, or depends on a constant, you might as well define a class method: class SomeClass CONFIG = "~/.foo.rc" def self.config_exists? File.exists?(CONFIG) end def init raise "Config file not found" unless SomeClass.config_exists? ... end end > end > How do you deal with this kind of thing? Depends on the context. -- Anurag Priyam http://about.me/yeban/