'-' an undifined method :S
Author |
Message |
Clayton
|
Posted: Thu Feb 15, 2007 1:44 pm Post subject: '-' an undifined method :S |
|
|
ok, I'm doing an assignment in Ruby, and we have to create a Script Generator for B Westerns. Basically I've got an Actor class and a Cowboy class which inherits it. I'm trying to do a little interaction right now, and I came upon this problem:
code: |
in 'shoot': undefined method '-' for nil:NilClass (NoMethodError)
|
and I have no idea why. any help would be greatly appreciated, my code so far:
Ruby: |
#the script generator program, yay...
#done in Ruby of course.
class Actor
attr_reader :name, :emotion, :status
def initialize(name, emotion)
@name = name
@emotion = emotion
@status = "alive"
end
def speak(text)
puts @name + ": " + text
end
#only a cowboy/girl and it's children shall be passed
def be_shot_by(an_actor)
@emotion = "scared shitless"
@status = "dead"
an_actor.shoot(self)
puts "Bang!"
speak("you... shot me.... (Dies)")
end
def be_threatened_by(an_actor)
@emotion = "terrified"
speak("Ok... I don't want any trouble, please don't hurt me!")
end
end
class Cowboy < Actor
#A cowboy's favorite weapon, the six shooter, has 6 bullets
@six_shooter = 6
def shoot(an_actor)
puts "(" + @name + " shoots at " + an_actor.name + ")" #if @six_shooter > 0
@six_shooter = @six_shooter - 1
end
def be_shot_by(an_actor)
@emotion = "calm cool and collected"
if @six_shooter > 0
self.shoot(an_actor)
speak("Ha ha ha! You think you can shoot at me and get away with it!?")
end
end
end
actor1 = Actor.new("Bob", "timid")
cowboy1 = Cowboy.new("Jesse James", "leader")
actor1.be_shot_by(cowboy1)
|
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
wtd
|
Posted: Thu Feb 15, 2007 2:08 pm Post subject: RE:\'-\' an undifined method :S |
|
|
Simple. In the shoot method, the @six_shooter instance variable is nil.
Yet, you would surely assert that you did in fact assign a value to it.
Consider that in Ruby, classes are objects. |
|
|
|
|
|
Cervantes
|
Posted: Thu Feb 15, 2007 3:23 pm Post subject: Re: '-' an undifined method :S |
|
|
wtd speaks truth. There's a difference in what happens when you define a class in Ruby and when you define a class in Turing. You've coded in the Turing style, assuming that the code inside the class is run every time a new object is created. But that's not the Ruby way.
Just read the beginning of the Dynamic Classes tutorial.
Now, ask yourself, where does the @six_shooter instance variable live? |
|
|
|
|
|
Clayton
|
Posted: Thu Feb 15, 2007 3:57 pm Post subject: Re: '-' an undifined method :S |
|
|
Okay, so @six_shooter lives inside the Object class, but where does that leave me? I'm still confused
I read the tutorial, but I'm still unclear how that applies here. |
|
|
|
|
|
ericfourfour
|
Posted: Thu Feb 15, 2007 5:03 pm Post subject: RE:\'-\' an undifined method :S |
|
|
Couldn't you just slap an initialize method into Cowboy, call super and initialize six_shooter? |
|
|
|
|
|
Clayton
|
Posted: Thu Feb 15, 2007 5:10 pm Post subject: Re: '-' an undifined method :S |
|
|
that's actually exactly what I did, it was just talked about in the IRC channel, much more convenient |
|
|
|
|
|
|
|