Forum: Ruby Beginning Ruby

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7ce0edb8e58df0b09ab65786140fe638?d=identicon&s=25 Kash Mr (champloo0)
on 2014-08-06 05:35
Been reading a tutorial ruby book, and i'm not sure if this error is
caused by a typo in the book, or some logical error only the experienced
would know how to correct

/code//

class Square
def intialize
  if defined?(@@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
b = Square.new
puts Square.count

///

Here's the error:

///
>ruby Square.rb
Square.rb:11:in `count': uninitialized class variable
@@number_of_squares in Square (NameError)
  from Square.rb:17
>Exit code: 1
>ruby Square.rb
Square.rb:10: undefined local variable or method `s' for Square:Class
(NameError)
>Exit code: 1
>ruby Square.rb
Square.rb:11:in `count': uninitialized class variable
@@number_of_squares in Square (NameError)
  from Square.rb:17
>Exit code: 1
---------------------------------------------------------------------------
The triple slashes(///) aren't part of the code, just to make reader
friendly

Many thanks in advanced.
4a65f01f7ece0b720bdb0de3c3db089e?d=identicon&s=25 Dansei Yuuki (blutorange)
on 2014-08-06 21:20
Just correct the typo.

> def intialize

should be

> def initialize

And it works. ; )
7ce0edb8e58df0b09ab65786140fe638?d=identicon&s=25 Kash Mr (champloo0)
on 2014-08-06 21:50
Lol thanks I can't believe i didn't notice that.
This topic is locked and can not be replied to.