Computer Science Canada '-' an undifined method :S |
Author: | Clayton [ 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:
and I have no idea why. any help would be greatly appreciated, my code so far:
|
Author: | wtd [ 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. |
Author: | Cervantes [ 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? |
Author: | Clayton [ 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. |
Author: | ericfourfour [ 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? |
Author: | Clayton [ 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 ![]() |