From: John Feminella Date: 2011-02-23T21:55:37+09:00 Subject: Re: Calling class method from instance method First, write your class method so that it accepts an instance: ==== begin snippet ==== class ExtendedFile attr_accessor :filename def self.process(file) if file.exists? # do some work end end def exists? self.exists? self.filename end end ==== end snippet ==== Of course, once you can do that, then you can refactor the class method into an instance method. Delete the method's argument and replace it with a reference to `self`: ==== begin snippet ==== class ExtendedFile attr_accessor :filename def process # now it's an instance method if self.exists? # reference self instead of the argument # do some work end end def exists? self.exists? self.filename end end ==== end snippet ==== ~ jf -- John Feminella Principal Consultant, BitsBuilder LI: http://www.linkedin.com/in/johnxf SO: http://stackoverflow.com/users/75170/ On Wed, Feb 23, 2011 at 07:40, marco antonio f. wrote: > Ryan Davis , > "When you want to have a class call an "instance method", which instance > would call it?" > > Good point! Makes sense. > > Anurag Priyam, this is pretty wierd... but works. > > 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 > >  def file_exists? >    #there are instance methods depending on this one >    File.exists?("some_file_name.txt") >  end > end > > How do you deal with this kind of thing? > > -- > Posted via http://www.ruby-forum.com/. > >