Forum: Ruby Beginning Ruby

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.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.