Class method access instance variable?

Discussion in 'Ruby' started by Finn Koch, Aug 23, 2007.

  1. Finn Koch

    Finn Koch Guest

    Hi, I'm just learning ruby and I'm working on a server connection. I
    have the following code:

    class IRCBot

    ....


    def connect
    puts "Connecting to #{@server}..."
    @conn = TCPSocket.new( @server, @port )
    handle_server_registration
    end
    end


    I have another class that needs to perform a '@conn.send("asdf",0)'.
    Can anyone help me figure out how I can have this external class access
    the @conn instance variable?

    Thanks!

    upenox
    --
    Posted via http://www.ruby-forum.com/.
     
    Finn Koch, Aug 23, 2007
    #1
    1. Advertising

  2. Finn Koch

    Finn Koch Guest

    Daniel ----- wrote:
    > On 8/23/07, Finn Koch <> wrote:
    >> puts "Connecting to #{@server}..."
    >> Thanks!
    >>
    >> upenox
    >> --

    >
    >
    > Don't access the @conn instance variable directly.
    >
    > Use an attr_accessor in the class IRCBot and the just ask for the
    > @irc_bot_instance.conn
    >
    > HTH
    > Daniel


    Thanks for the response :)

    I have tried this, with no luck. I have an external file that creates
    the new instance of IRCBot:

    mybot = IRCBot.new


    I tried referring to it as 'mybot.conn.send("asdf", 0)', but that didn't
    work.

    Here is the external file that gets loaded:

    class IRCCallback
    def self.check_next( input )
    mybot.conn.send("PRIVMSG matt-mb :hey-o", 0)
    puts "irc callback working"
    end

    end

    So I guess to sum up, I have mybot.rb which contains the "mybot =
    IRCBot.new", the IRCBot.rb which contains the method in my original
    post, and I have the IRCCallback.rb which gets loaded prior to calling
    the function which in turns calls the method in IRCBot.rb.

    Thanks again! :)
    --
    Posted via http://www.ruby-forum.com/.
     
    Finn Koch, Aug 23, 2007
    #2
    1. Advertising

  3. Hi --

    On Thu, 23 Aug 2007, Finn Koch wrote:

    > Hi, I'm just learning ruby and I'm working on a server connection. I
    > have the following code:
    >
    > class IRCBot
    >
    > ....
    >
    >
    > def connect
    > puts "Connecting to #{@server}..."
    > @conn = TCPSocket.new( @server, @port )
    > handle_server_registration
    > end
    > end
    >
    >
    > I have another class that needs to perform a '@conn.send("asdf",0)'.
    > Can anyone help me figure out how I can have this external class access
    > the @conn instance variable?


    It can't, at least not directly. However, Ruby provides a very easy
    way to wrap instance variables in accessor (get/set) methods. In your
    case, it would be something like this:

    class IRCBot
    attr_reader :conn # "getter" (reader) method wrapped around @conn

    def connect
    puts ...
    # etc.
    end
    end

    Now you can do:

    class OtherClass
    def whatever
    bot = IRCBot.new
    bot.conn... # you now have access to bot's conn attribute
    end
    end

    Another way to put this is: Ruby objects can have attributes, which
    are readable and/or writeable. From the outside, these are just
    methods. From the inside (the class where they're defined), they are
    implemented (unless you write a fancier custom version) as wrapper
    methods around instance variables.


    David

    --
    * Books:
    RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
    RUBY FOR RAILS (http://www.manning.com/black)
    * Ruby/Rails training
    & consulting: Ruby Power and Light, LLC (http://www.rubypal.com)
     
    David A. Black, Aug 23, 2007
    #3
  4. Finn Koch

    Finn Koch Guest

    Daniel ----- wrote:
    > On 8/23/07, Finn Koch <> wrote:
    >> > Don't access the @conn instance variable directly.

    >> the new instance of IRCBot:
    >> def self.check_next( input )
    >>
    >> Thanks again! :)
    >> --
    >> Posted via http://www.ruby-forum.com/.

    >
    >
    > Can you http://pastie.caboo.se/ your relevant code. In it's entirety.
    > I
    > can't see where in IRCCallback mybot is defined etc.
    >
    > It would sure help to see these.
    >
    > Thanx
    > Daniel



    http://pastie.caboo.se/90341

    --
    Posted via http://www.ruby-forum.com/.
     
    Finn Koch, Aug 23, 2007
    #4
  5. Hi --

    On Thu, 23 Aug 2007, Finn Koch wrote:

    > Daniel ----- wrote:
    >> On 8/23/07, Finn Koch <> wrote:
    >>>> Don't access the @conn instance variable directly.
    >>> the new instance of IRCBot:
    >>> def self.check_next( input )
    >>>
    >>> Thanks again! :)
    >>> --
    >>> Posted via http://www.ruby-forum.com/.

    >>
    >>
    >> Can you http://pastie.caboo.se/ your relevant code. In it's entirety.
    >> I
    >> can't see where in IRCCallback mybot is defined etc.
    >>
    >> It would sure help to see these.
    >>
    >> Thanx
    >> Daniel

    >
    >
    > http://pastie.caboo.se/90341


    In this:

    class IRCCallback
    def self.check_next( input )
    ro.conn.send("PRIVMSG testuser :hey", 0)
    puts "irc callback working"
    end
    end

    I don't see where ro is being defined.


    David

    --
    * Books:
    RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
    RUBY FOR RAILS (http://www.manning.com/black)
    * Ruby/Rails training
    & consulting: Ruby Power and Light, LLC (http://www.rubypal.com)
     
    David A. Black, Aug 23, 2007
    #5
  6. Finn Koch

    Finn Koch Guest

    David A. Black wrote:
    > Hi --
    >
    > On Thu, 23 Aug 2007, Finn Koch wrote:
    >
    >>>

    >> http://pastie.caboo.se/90341

    > In this:
    >
    > class IRCCallback
    > def self.check_next( input )
    > ro.conn.send("PRIVMSG testuser :hey", 0)
    > puts "irc callback working"
    > end
    > end
    >
    > I don't see where ro is being defined.
    >
    >
    > David


    Oh, sorry, that should read 'mybot.conn.send("PRIVMSG testuser :hey",
    0)'

    That's what I was trying last night.
    --
    Posted via http://www.ruby-forum.com/.
     
    Finn Koch, Aug 23, 2007
    #6
  7. Hi --

    On Thu, 23 Aug 2007, Finn Koch wrote:

    > David A. Black wrote:
    >> Hi --
    >>
    >> On Thu, 23 Aug 2007, Finn Koch wrote:
    >>
    >>>>
    >>> http://pastie.caboo.se/90341

    >> In this:
    >>
    >> class IRCCallback
    >> def self.check_next( input )
    >> ro.conn.send("PRIVMSG testuser :hey", 0)
    >> puts "irc callback working"
    >> end
    >> end
    >>
    >> I don't see where ro is being defined.
    >>
    >>
    >> David

    >
    > Oh, sorry, that should read 'mybot.conn.send("PRIVMSG testuser :hey",
    > 0)'


    OK... (well, not OK :) but I now know what you meant) but I'm now not
    seeing what purpose the variable 'input' is serving.

    > That's what I was trying last night.


    mybot is a local variable defined in a completely different scope,
    different both because method definitions have their own local scope,
    and because it's in a different file, either of which would mean it
    was out of scope in your method definition.

    You need to pass objects around, and make requests of those objects
    (i.e., send them messages). Local variables are really just scratchpad
    variables for a limited scope.


    David

    --
    * Books:
    RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
    RUBY FOR RAILS (http://www.manning.com/black)
    * Ruby/Rails training
    & consulting: Ruby Power and Light, LLC (http://www.rubypal.com)
     
    David A. Black, Aug 23, 2007
    #7
  8. Finn Koch

    Finn Koch Guest

    David A. Black wrote:
    > Hi --
    >
    > On Thu, 23 Aug 2007, Finn Koch wrote:
    >
    >>> def self.check_next( input )

    >> Oh, sorry, that should read 'mybot.conn.send("PRIVMSG testuser :hey",
    >> 0)'

    >
    > OK... (well, not OK :) but I now know what you meant) but I'm now not
    > seeing what purpose the variable 'input' is serving.
    >
    >> That's what I was trying last night.

    >
    > mybot is a local variable defined in a completely different scope,
    > different both because method definitions have their own local scope,
    > and because it's in a different file, either of which would mean it
    > was out of scope in your method definition.
    >
    > You need to pass objects around, and make requests of those objects
    > (i.e., send them messages). Local variables are really just scratchpad
    > variables for a limited scope.
    >
    >
    > David


    Well the 'input' var isn't serving a purpose now. I'm just trying to
    get the callbacks and everything working and will be using input at a
    later time.

    So I could pass @conn to the check_next like so?

    IRCCallback.check_next(@conn, @username)

    And then have:

    class IRCCallback
    def check_next(server_connection, username)
    do stuff
    end
    end

    Does that look correct?
    --
    Posted via http://www.ruby-forum.com/.
     
    Finn Koch, Aug 23, 2007
    #8
  9. * Finn Koch <> (04:38) schrieb:

    > I have tried this, with no luck. I have an external file that creates
    > the new instance of IRCBot:


    Stop thinking of files, Ruby is about objects, it doesn't in which files
    statements are, as long as they are processed.

    > mybot = IRCBot.new


    mybot is a local variable now.

    > I tried referring to it as 'mybot.conn.send("asdf", 0)', but that didn't
    > work.


    What do you mean, what happended? Does IRCBot have an conn method? What
    happens if you use mybot.conn? [1]

    > class IRCCallback
    > def self.check_next( input )
    > mybot.conn.send("PRIVMSG matt-mb :hey-o", 0)
    > puts "irc callback working"
    > end
    >
    > end


    There is no local variable mybot here. Call it $mybot if you want
    global variables, but you really shouldn't want that.

    Try:

    class IRCCallback
    attr_accessor :bot

    def self.check_next( input )
    @bot.conn.send("PRIVMSG matt-mb :hey-o", 0)
    puts "irc callback working"
    end
    end

    IRCCallback.bot = IRCBot.new

    mfg, simon .... l

    [1] Why the hell did BasicSocket redefine Object#send?
     
    Simon Krahnke, Aug 23, 2007
    #9
    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. Gerry Sutton
    Replies:
    1
    Views:
    581
    Peter Otten
    Apr 16, 2005
  2. David Garamond
    Replies:
    5
    Views:
    276
    Ara.T.Howard
    Jun 8, 2004
  3. Raj Singh
    Replies:
    2
    Views:
    217
    Rick DeNatale
    May 29, 2008
  4. Greg Hauptmann
    Replies:
    9
    Views:
    268
    Loren Segal
    Jun 16, 2008
  5. Chuck Remes
    Replies:
    3
    Views:
    210
    hemant
    Aug 12, 2010
Loading...

Share This Page