From: Mayank Kohaley Date: 2011-03-07T21:03:22+09:00 Subject: Re: basic programming question, help please --001636c92ae10d40d0049de348ed Content-Type: text/plain; charset=ISO-8859-1 The class method in Ruby is represented using self. or ., A class method is used to modify the class variables( which has the same value in every object of that class, as you have in your program @@number_of_squares). On Mon, Mar 7, 2011 at 5:09 PM, Kaye Ng wrote: > class Square > def initialize > if defined?(@@number_of_squares) > @@number_of_squares = @@number_of_squares + 1 > else > @@number_of_squares = 1 > end > end > def self.count > @@number_of_squares > end > end > > a = Square.new #would return 1 > b = Square.new #would return 2 > puts Square.count #2 would be printed on the screen > > -------------------------------------------------------------------- > > My question is: why is the "self" necessary in "def self.count". > I don't understand the logic behind it. > > (I know that I can also type "def Square.count" instead of "def > self.count") > > Thanks guys! > > -- > Posted via http://www.ruby-forum.com/. > > --001636c92ae10d40d0049de348ed--