Problem with object methods?

C

Carter Davis

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?
 
H

Hugh Sasse

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.
Hugh
 
C

Carter Davis

Okay, I made an example.

class FirstClass
def initialize
puts "YAY INITIALIZE!"
end

text

def text
puts "Yay more text!"
end
end

firstClass = FirstClass.new

Now, when I run this, it only uses the initialize method, not the text
method. Is there any way that I can use the text method?
 
H

Hugh Sasse

Okay, I made an example.

I suggest you change it like this to see what is going on.

puts "before class : #{self.inspect}"
class FirstClass
def initialize
puts "YAY INITIALIZE!"
puts "in initialize : #{self.inspect}"
puts "inside class : #{self.inspect}"
# I'd have expected an error for that call to test. try
# commenting it out for now...
def text
puts "Yay more text!"
puts "inside test method : #{self.inspect}"
end
end

firstClass = FirstClass.new firstClass.test

Now, when I run this, it only uses the initialize method, not the text
method. Is there any way that I can use the text method?

Hugh
 
C

Carter Davis

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)
 
D

David A. Black

Hi --

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?


David

--
Rails training from David A. Black and Ruby Power and Light:
Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL
Advancing with Rails January 19-22 Fort Lauderdale, FL *
* Co-taught with Patrick Ewing!
See http://www.rubypal.com for details and updates!
 
C

Carter Davis

The text of the program was as follows:

puts "before class : #{self.inspect}"

class FirstClass
def initialize
puts "YAY INITIALIZE!"
puts "in initialize : #{self.inspect}"
end
puts "inside class : #{self.inspect}"
# text

def text
puts "Yay more text!"
puts "inside test method : #{self.inspect}"
end
end

firstClass = FirstClass.new
firstClass.test
 
D

David A. Black

Hi --

The text of the program was as follows:

puts "before class : #{self.inspect}"

class FirstClass
def initialize
puts "YAY INITIALIZE!"
puts "in initialize : #{self.inspect}"
end
puts "inside class : #{self.inspect}"
# text

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).


David

--
Rails training from David A. Black and Ruby Power and Light:
Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL
Advancing with Rails January 19-22 Fort Lauderdale, FL *
* Co-taught with Patrick Ewing!
See http://www.rubypal.com for details and updates!
 
C

Carter Davis

All I want is to be able to call upon other methods besides the
initialize method.
 
D

David A. Black

Hi --

All I want is to be able to call upon other methods besides the
initialize method.

Whoops, my last answer was partly wrong, because I misread firstClass
as FirstClass (darn camelCase! :) The part about 'test' was right,
though.

Basically, if you change 'test' to 'text' it should work.


David

--
Rails training from David A. Black and Ruby Power and Light:
Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL
Advancing with Rails January 19-22 Fort Lauderdale, FL *
* Co-taught with Patrick Ewing!
See http://www.rubypal.com for details and updates!
 
H

Hugh Sasse

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 .

Does that answer your question(s)?

(NoMethodError)
Hugh
 
C

Carter Davis

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?
 
S

Siep Korteling

Carter said:
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
end

firstClass = FirstClass.new
firstClass.text

hth,

Siep
 
H

Hugh Sasse

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.]
Try this:

class FirstClass
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.
end

def method
puts "This is what I want it to show."
end
end

firstClass = FirstClass.new
firstClass.text

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 :)
HTH
Hugh
 
C

Carter Davis

MY GOD.

It worked in the example, but URGH.

It threw this error when I ran my new, modified application I;ve been
working on:

test.rb:33:in `room_one': undefined method `verify1' for
#<Room1:0x27948> (NoMethodError)
from test.rb:25:in `initialize'
from test.rb:69:in `new'
from test.rb:69

I don't know why! I did everything...
 
H

Hugh Sasse

MY GOD.

It worked in the example, but URGH.

It threw this error when I ran my new, modified application I;ve been
working on:

test.rb:33:in `room_one': undefined method `verify1' for
#<Room1:0x27948> (NoMethodError)
from test.rb:25:in `initialize'
from test.rb:69:in `new'
from test.rb:69

I don't know why! I did everything...

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

puts [room_one.methods - Object.instance_methods].sort.inspect

which will give you a list of methods that room_one doesn't inherit
from Object, which will include those you defined yourself.

Also, remember room_one.class will return the class of room_one so you
can see if that is correct.

Hugh
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top