K
Kaye Ng
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!
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!