[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Problem with object methods?

Carter Davis

11/10/2008 1:49:00 PM

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?
--
Posted via http://www.ruby-....

15 Answers

Hugh Sasse

11/10/2008 2:11:00 PM

0



On Mon, 10 Nov 2008, Carter Davis wrote:

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

> --
> Posted via http://www.ruby-....
>
Hugh

Carter Davis

11/10/2008 2:32:00 PM

0

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?
--
Posted via http://www.ruby-....

Hugh Sasse

11/10/2008 3:18:00 PM

0



On Mon, 10 Nov 2008, Carter Davis wrote:

> 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}"
> end
>
puts "inside class : #{self.inspect}"
> text
# 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?
> --
> Posted via http://www.ruby-....
>

Hugh

Carter Davis

11/10/2008 7:57:00 PM

0

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)

--
Posted via http://www.ruby-....

David A. Black

11/10/2008 8:04:00 PM

0

Hi --

On Tue, 11 Nov 2008, Carter Davis wrote:

> 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.r... for details and updates!

Carter Davis

11/10/2008 8:10:00 PM

0

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

--
Posted via http://www.ruby-....

David A. Black

11/10/2008 8:21:00 PM

0

Hi --

On Tue, 11 Nov 2008, Carter Davis wrote:

> 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.r... for details and updates!

Carter Davis

11/10/2008 8:24:00 PM

0

All I want is to be able to call upon other methods besides the
initialize method.
--
Posted via http://www.ruby-....

David A. Black

11/10/2008 8:30:00 PM

0

Hi --

On Tue, 11 Nov 2008, Carter Davis wrote:

> 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.r... for details and updates!

Hugh Sasse

11/10/2008 9:32:00 PM

0



On Tue, 11 Nov 2008, Carter Davis wrote:

> 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