Problem with object methods?

Discussion in 'Ruby' started by Carter Davis, Nov 10, 2008.

  1. Carter Davis

    Carter Davis Guest

    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-forum.com/.
    Carter Davis, Nov 10, 2008
    #1
    1. Advertising

  2. Carter Davis

    Hugh Sasse Guest

    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-forum.com/.
    >

    Hugh
    Hugh Sasse, Nov 10, 2008
    #2
    1. Advertising

  3. Carter Davis

    Carter Davis Guest

    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-forum.com/.
    Carter Davis, Nov 10, 2008
    #3
  4. Carter Davis

    Hugh Sasse Guest

    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-forum.com/.
    >


    Hugh
    Hugh Sasse, Nov 10, 2008
    #4
  5. Carter Davis

    Carter Davis Guest

    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-forum.com/.
    Carter Davis, Nov 10, 2008
    #5
  6. 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.rubypal.com for details and updates!
    David A. Black, Nov 10, 2008
    #6
  7. Carter Davis

    Carter Davis Guest

    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-forum.com/.
    Carter Davis, Nov 10, 2008
    #7
  8. 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.rubypal.com for details and updates!
    David A. Black, Nov 10, 2008
    #8
  9. Carter Davis

    Carter Davis Guest

    Carter Davis, Nov 10, 2008
    #9
  10. 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.rubypal.com for details and updates!
    David A. Black, Nov 10, 2008
    #10
  11. Carter Davis

    Hugh Sasse Guest

    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
    Hugh Sasse, Nov 10, 2008
    #11
  12. Carter Davis

    Carter Davis Guest

    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?
    --
    Posted via http://www.ruby-forum.com/.
    Carter Davis, Nov 10, 2008
    #12
  13. Carter Davis wrote:
    > 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

    --
    Posted via http://www.ruby-forum.com/.
    Siep Korteling, Nov 10, 2008
    #13
  14. Carter Davis

    Hugh Sasse Guest

    On Tue, 11 Nov 2008, Siep Korteling wrote:

    > Carter Davis wrote:
    > > 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


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


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


    HTH
    Hugh
    Hugh Sasse, Nov 10, 2008
    #14
  15. Carter Davis

    Carter Davis Guest

    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...
    --
    Posted via http://www.ruby-forum.com/.
    Carter Davis, Nov 11, 2008
    #15
  16. Carter Davis

    Hugh Sasse Guest

    On Tue, 11 Nov 2008, Carter Davis wrote:

    > 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
    Hugh Sasse, Nov 11, 2008
    #16
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neo
    Replies:
    1
    Views:
    516
    Scott Allen
    Jan 7, 2005
  2. Buster Copley
    Replies:
    5
    Views:
    556
    Gianni Mariani
    Jul 7, 2003
  3. Nikita Petrov
    Replies:
    2
    Views:
    110
    Gary Wright
    Apr 6, 2008
  4. Kenneth McDonald
    Replies:
    5
    Views:
    312
    Kenneth McDonald
    Sep 26, 2008
  5. torbs
    Replies:
    1
    Views:
    126
Loading...

Share This Page