
January 16th, 2012, 05:33 PM
|
|
Registered User
|
|
Join Date: Jan 2012
Posts: 2
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.
|