From: 7stud -- Date: 2007-11-01T09:03:28+09:00 Subject: Re: why, or when to, use a class method? Thufir wrote: > On Oct 31, 2:17 pm, 7stud -- wrote: > [...] >> > Why use a class method in the first example, though? >> >> So that you can check whether a Song is too long without having to >> create a SongList object: > [...] > > What would be a case where there's a SongList object, though? class Song attr_accessor :duration def initialize(len) @duration = len end end class SongList MaxTime = 5*60 attr_accessor :songs def initialize(song_arr) @songs = song_arr end def isTooLong(aSong) return aSong.duration > MaxTime end end my_playlist = SongList.new([Song.new(10), Song.new(4)]) song = my_playlist.songs[0] puts my_playlist.isTooLong(song) > Or, if > there are class methods then generally there won't be objects of that > class? > If you will never have a need to create objects of the class, then I think you might just create a Module. On the other hand creating a class where you can call class methods without objects and create objects and call instance methods gives you more options. More options allows more flexibility. -- Posted via http://www.ruby-forum.com/.