Forum: Ruby String constant reference to another class instance variable

Posted by aaaa aaaaaaaaaaa (phora)
on 2010-02-08 14:36
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 by Robert Klemme (Guest)
on 2010-02-08 15:17
(Received via mailing list)
2010/2/8 Mikkel Kroman <mk@maero.dk>:
>    @socket = TCPSocket.new
>  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)?

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
Posted by Brian Candler (candlerb)
on 2010-02-08 15:47
class String
  attr_accessor :socket
end

But as Robert already said, you do not want to do this.
Posted by aaaa aaaaaaaaaaa (phora)
on 2010-02-08 15:59
Thanks for both your answer, but what I want to achieve is something 
like the following:

(This is a early stage irc lib I'm using)

#!/usr/bin/env ruby
# encoding: utf-8
require File.dirname(__FILE__) + '/lib/irc'

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

nick is a string and I think it's too exaggerated to make nick have it's 
own kind of class, just to have this method. Oh, and another thing..

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.

Sincerely,
Mikkel Kroman.
Posted by Robert Klemme (Guest)
on 2010-02-08 16:31
(Received via mailing list)
2010/2/8 Mikkel Kroman <mk@maero.dk>:
>  def on_message(nick, channel, message, *args)
> nick is a string and I think it's too exaggerated to make nick have it's
> own kind of class, just to have this method. Oh, and another thing..

I would not say that.  A nick could have other methods as well, e.g.
#state, #online? etc.  Actually it's not so much a nick but rather a
User or a Chatter.  Software engineering is all about finding proper
abstractions. :-)

> 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.  What's best depends of course on your application
design which we don't know (yet).  Maybe you lay out your design - at
least on the high level - and then we can comment further.

Kind regards

robert
Posted by Robert Klemme (Guest)
on 2010-02-08 16:39
(Received via mailing list)
2010/2/8 Robert Klemme <shortcutter@googlemail.com>:
> 2010/2/8 Mikkel Kroman <mk@maero.dk>:
>> 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.  What's best depends of course on your application
> design which we don't know (yet).  Maybe 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
Posted by Brian Candler (candlerb)
on 2010-02-08 18:56
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)
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.