#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2012
    Posts
    2
    Rep Power
    0

    Problem with class variables


    I'm trying to learn Ruby by reading "Humble Little Ruby" book and yesterday I got stuck and wondering if anyone here can help me.
    Basically the code creates a class "boogeyman" with several methods, right now the author is explaining class variables, so he creates one in the boogeyman class to keep track of which instance of boogeyman was created last by user. I copied the code word to word, but still get an error:
    undefined method `latest' for Boogeyman:Class (NoMethodError)
    Here's the code itself:

    Code:
    class Boogeyman
      attr_writer :scare_factor
      
      def initialize(name, location)
        @name = name
        @location = location
        
        @@latest = @name
        @@location = @location
        puts "Yes, master?"
      end
      
      def change_location(new_location)
        @location = new_location
        puts "I moved to #{new_location}!"
        self.get_info
      end
      
      def change_name(new_name)
        @name = new_name
        puts "I shall be called #{new_name} from now on."
        self.get_info
      end
      
      def get_info
        puts "I'm #{@name} in #{@location}."
      end
      
      def scare_factor
        return "#{@scare_factor / 1000}Fg"
      end
      
    
      protected
      def are_you_a_monster?(whosasking)
        puts "Yes #{whosasking}, I'm a monster!"
        return true
      end
      
      private
      def phone_home(message)
        puts message
      end
      
      public
      def scare(who)
        phone_home("I just scared the living **** out of #{who}")
      end
    end
    
    #test the class
    monster1 = Boogeyman.new("Crazy Cal", "Nashville, TN")
    monster2 = Boogeyman.new("Gory Gary", "Reston, WV")
    puts Boogeyman.latest
    #monster1.scare("bitch")
    #monster1.scare_factor = 6000
    #puts monster1.scare_factor
    #monster1.change_location("Wyoming")
    #monster1.change_name("Beezlebub")
    Class variable is create in initialize method.
    Thanks for the help in advance.
  2. #2
  3. Commie Mutant Traitor
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2004
    Location
    Alpharetta, GA
    Posts
    1,806
    Rep Power
    1570
    The problem here is that class variables are private to the class in question; thus, trying to access the variable Bogeyman.latest will cause this error, since while the variable has been created and set, it isn't visible from outside of the class.

    Worse, you cannot have an attribute for the class vars either, as attributes are part of the object, not the class. There are extensions in Rails for class attributes, but in plain Ruby, you can't access the class variables from outside of the class except through a class method.

    I'm not sure what the book's author had in mind when he or she wrote that; it may have been intentional, to see if you picked up on the error, in which case it will probably say so later in the text.
    Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
    #define KINSEY (rand() % 7) λ Scheme is the Red Pill
    Scheme in Short Understanding the C/C++ Preprocessor
    Taming Python A Highly Opinionated Review of Programming Languages for the Novice, v1.1

    FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov

IMN logo majestic logo threadwatch logo seochat tools logo