SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Oct 2008
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    why do I get an error

    Code:
    class Person
      @@count = 0
      attr_accessor :first_name
      attr_accessor :last_name
      attr_accessor :date_of_birth
      
      def self.count
        @@count
      end
      
      def initialize(name = nill)
        @first_name, @last_name = name.split(" ")
        @first_name.capitalize
        @last_name.downcase.capitalize
        @@count+=1
      end
      
      def age
        @month, @day, @year= date_of_birth.split('/')
        t = Time.now
        @year - t.year
      end
    end
    
    P = Person.new("James Lowe")
    P.count
    I get an error when I try to run the count method.

  2. #2
    l 0 l silver trophybronze trophy lo0ol's Avatar
    Join Date
    Aug 2002
    Location
    Palo Alto
    Posts
    5,329
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You defined count as a class method. Invoke it using Person.count instead (though in your case you likely want to change it from self.count to just count and call it as p.count).

  3. #3
    SitePoint Member
    Join Date
    Oct 2008
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •