Ruby Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesRuby Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 16th, 2012, 05:33 PM
haosmark haosmark is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2012
Posts: 2 haosmark User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 26 m 22 sec
Reputation 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:
Quote:
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.

Reply With Quote
  #2  
Old January 17th, 2012, 10:28 AM
Schol-R-LEA's Avatar
Schol-R-LEA Schol-R-LEA is offline
Commie Mutant Traitor
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jun 2004
Location: Norcross, GA (again)
Posts: 1,759 Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 3 h 38 m 3 sec
Reputation Power: 1568
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 ShortUnderstanding the C/C++ Preprocessor
Taming PythonA 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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesRuby Programming > Problem with class variables

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap