String constant reference to another class instance variable

Discussion in 'Ruby' started by Mikkel Kroman, Feb 8, 2010.

  1. Hello.

    How can I be able to do something like this:

    class Connection
    attr_accessor :socket, :name

    def initialize name
    @name = name
    end

    def connect
    @socket = TCPSocket.new
    end

    def saysomething
    'phora'.say('hi!')
    end
    end

    class String
    def say message
    @socket.puts("#{self} says #{message}")
    end
    end

    conn = Connection.new "Mikkel"
    conn.connect
    conn.saysomething

    String.socket = conn.socket # Something like this…

    - Without having to use 'phora'.say('hi!', @socket)?

    Sincerely,
    Mikkel Kroman.
    --
    Posted via http://www.ruby-forum.com/.
     
    Mikkel Kroman, Feb 8, 2010
    #1
    1. Advertising

  2. 2010/2/8 Mikkel Kroman <>:
    > How can I be able to do something like this:
    >
    > class Connection
    > =A0attr_accessor :socket, :name
    >
    > =A0def initialize name
    > =A0 =A0@name =3D name
    > =A0end
    >
    > =A0def connect
    > =A0 =A0@socket =3D TCPSocket.new
    > =A0end
    >
    > =A0def saysomething
    > =A0 =A0'phora'.say('hi!')
    > =A0end
    > end
    >
    > class String
    > =A0def say message
    > =A0 =("#{self} says #{message}")
    > =A0end
    > end
    >
    > conn =3D Connection.new "Mikkel"
    > conn.connect
    > conn.saysomething
    >
    > String.socket =3D conn.socket # Something like this=85
    >
    > - Without having to use 'phora'.say('hi!', @socket)?


    Frankly, you do not want to be doing this. First of all String's
    capabilities aren't really in the area of socket communication. Class
    String is responsible for manipulating strings in various ways but not
    for doing IO.

    Then, making possible what you want to do will make your code hard to
    impossible to read because you have *implicit* transfer of
    information. These things are hard to understand and consequently
    hard to use and debug.

    Since you do have your simple abstraction already (method
    Connection#saysomething) you should stick with that. Btw, I would add
    at least one parameter to #saysomething, namely the thing you want to
    say.

    Kind regards

    robert


    --=20
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, Feb 8, 2010
    #2
    1. Advertising

  3. 2010/2/8 Robert Klemme <>:
    > 2010/2/8 Mikkel Kroman <>:
    >> How would I be able to create User instances which also should have
    >> access to the IRC::Client's socket? Currently I'm using User.new("nick",
    >> @socket) which is, well.. yeah.

    >
    > You can do that. =A0What's best depends of course on your application
    > design which we don't know (yet). =A0Maybe you lay out your design - at
    > least on the high level - and then we can comment further.


    One more remark: if I would be doing this I would implement this with
    (at least) two layers. First, I'd look at the IRC protocol and
    implement classes that abstract this protocol. Then I'd create a
    "user friendly" layer. In that scenario a socket would not be seen to
    a User (or Nick) class because sockets would be buried in the IRC
    protocol layer.

    Kind regards

    robert


    --=20
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, Feb 8, 2010
    #3
  4. Mikkel Kroman wrote:
    > IRC::Connect hostname: 'irc.phora.net' do
    > def on_message(nick, channel, message, *args)
    > # This is what I want it to be like:
    > # nick.say("Hello there, #{nick}!")
    > # or atleast something like that.
    > # but for now, I'm stuck with this:
    > privmsg(nick, "Hello there, #{nick}!")
    > # is it maybe possible to make some
    > # kind of 'alias'?
    > end
    > end


    If you just want to send a message to user with nickname "foo", then
    privmsg("foo", "Hello")
    looks to be the right way to go about it.

    If you want to abstract away the concept of an "IRC user" then create an
    object for it. One of the great things about Ruby is that it's only a
    few lines.

    > How would I be able to create User instances which also should have
    > access to the IRC::Client's socket? Currently I'm using User.new("nick",
    > @socket) which is, well.. yeah.


    That seems like exactly the way to go about it, if you know at user
    creation time that user "nick" is always reachable through @socket. (But
    really @socket should be the IRC client connection object, rather than
    the raw socket)
    --
    Posted via http://www.ruby-forum.com/.
     
    Brian Candler, Feb 8, 2010
    #4
    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:
    544
    Peter Otten
    Apr 16, 2005
  2. marekw2143
    Replies:
    3
    Views:
    1,366
    marekw2143
    Jul 25, 2009
  3. Martin P. Hellwig
    Replies:
    1
    Views:
    377
    Martin P. Hellwig
    Mar 26, 2010
  4. David Garamond
    Replies:
    5
    Views:
    251
    Ara.T.Howard
    Jun 8, 2004
  5. Raj Singh
    Replies:
    2
    Views:
    200
    Rick DeNatale
    May 29, 2008
Loading...

Share This Page