String constant reference to another class instance variable

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 K…

2010/2/8 Mikkel K. [email protected]:

@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

class String
attr_accessor :socket
end

But as Robert already said, you do not want to do this.

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 K…

2010/2/8 Robert K. [email protected]:

2010/2/8 Mikkel K. [email protected]:

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

2010/2/8 Mikkel K. [email protected]:

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

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

Mikkel K. 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)