I recently made an object for a game I’m making. It uses the constructor
method, but it won’t use its other methods. I tried calling upon it by
inserting the method’s name into the object’s text, but it still won’t
call upon them.
Is there any special wayto call upon the methods, or something?
I recently made an object for a game I’m making. It uses the constructor
You mean that it has an initialize method, and you called new on the
class to create it? Or did you try something else?
method, but it won’t use its other methods. I tried calling upon it by
inserting the method’s name into the object’s text, but it still won’t
What do you mean by “inserting … the object’s text”?
call upon them.
Is there any special wayto call upon the methods, or something?
I think it would be simplest if you reduced this to the smallest
example that doesn’t behave how you expect. Then show us the code,
the results you got from running it, and tell us why you think that
is unexpected. Then we can see if your code expresses your intentions
correctly, if you assumptions are wrong, or if there is some other
problem. All rather tedious, but it’s probably quickest in the long
run.
I made all the changes to the test app and ran it, and here’s what it
gave me:
before class : main
inside class : FirstClass
YAY INITIALIZE!
in initialize : #FirstClass:0x28dac
example.rb:18: private method `test’ called for #FirstClass:0x28dac
(NoMethodError)
I made all the changes to the test app and ran it, and here’s what it
gave me:
before class : main
inside class : FirstClass
YAY INITIALIZE!
in initialize : #FirstClass:0x28dac
example.rb:18: private method `test’ called for #FirstClass:0x28dac
(NoMethodError)
I’m confused as between ‘text’ and ‘test’ in your example. Can you
re-post the entire thing that gave you the above output?
def text
puts “Yay more text!”
puts “inside test method : #{self.inspect}”
end
end
firstClass = FirstClass.new
firstClass.test
The test method you’re calling at the end is a pre-defined, private
method defined in Kernel. I’m not sure what you’re wanting or
expecting it to do. Do you mean “text”? That will fail with an unknown
method error, since you haven’t defined a method text on your actual
class object FirstClass (only on its instances).
Yeh, I think I understand. One last thing, though. Say I have this code:
class FirstClass
def initialize
puts “YAY INITIALIZE!”
end
def text
puts "Yay more text!"
firstClass.method
end
def method
puts "This is what I want it to show."
end
end
firstClass = FirstClass.new
firstClass.text
In which I want it to execute the “text” method, and then, right after
that, execute the “method” method. Now, if I were to run THIS code, it
would throw an error, saying that the ‘firstClass’ variable or method is
undefined.
How could I make the function execute another method within the same
object?
I made all the changes to the test app and ran it, and here’s what it
gave me:
before class : main
inside class : FirstClass
YAY INITIALIZE!
in initialize : #FirstClass:0x28dac
example.rb:18: private method `test’ called for #FirstClass:0x28dac
My mistake: I misread your method name as test when it is text.
s/s/x/; # as they say in the Unix world!
So before the class statement, your object is main (actually within
the class Object.
Between class…end (the matching end, your object self is FirstClass,
so that all method calls will go to that class first.
When you run the text [not test :-)] method, your object is FirstClass:0x23dac( that is, an instance of Firstclass). You call the
methods on it with .
Yeh, I think I understand. One last thing, though. Say I have this code:
class FirstClass
def initialize
puts “YAY INITIALIZE!”
end
def text
puts "Yay more text!"
firstClass.method
end
def method
puts "This is what I want it to show."
end
end
firstClass = FirstClass.new
firstClass.text
In which I want it to execute the “text” method, and then, right after
that, execute the “method” method. Now, if I were to run THIS code, it
would throw an error, saying that the ‘firstClass’ variable or method is
undefined.
How could I make the function execute another method within the same
object?
Try this:
class FirstClass
def initialize
puts “YAY INITIALIZE!”
end
def text
puts "Yay more text!"
method # or self.method
end
def method
puts "This is what I want it to show."
end
It threw this error when I ran my new, modified application I;ve been
working on:
test.rb:33:in room_one': undefined methodverify1’ for
#Room1:0x27948 (NoMethodError)
from test.rb:25:in initialize' from test.rb:69:innew’
from test.rb:69
The reason this won’t work is because this variable is not in scope
when the method is run. [To do something similar, i.e., use a variable
inside a “function” when you have declared it outside, you will need
to know about “closures”, but for now just stash that word away for
later.]
In which I want it to execute the “text” method, and then, right after
def initialize
puts “YAY INITIALIZE!”
end
def text
puts "Yay more text!"
method # or self.method
Which is exactly what I was going to write. Now, you notice that
method just looks like a variable. If you have wotsit = something
(as you can have meaningfully for the accessors ruby creates with
attr_accessor (you’ll find that later))
then ruby will interpret it as a local variable, sometimes. To make
sure it is a method use the self form, but most of the time you don’t
need to. That’s why I was showing you how self varies as you go through
the code. If this is confusing, don’t worry about it, it will make
sense
later.
hth,
Siep
This sort of thing has not changed since, oh, about the year 2000?
So you can find the free copy of the first edition of Programming Ruby
online, to get these basics into your head. Some things have changed
since, so a newer edition will help till 1.9.1 comes out, when you’ll
be better off with the latest one, same as the rest of us
We’d really need to see the code to debug this kind of thing.
All I can say from this is that you had an object room_one of class
Room1
and it failed to find the method verify1 when you tried to call that
from
inside the initialize method, of something.
Useful things to print out for yourself could include