String constant reference to another class instance variable

M

Mikkel Kroman

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

Robert Klemme

2010/2/8 Mikkel Kroman said:
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 [email protected]("#{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/
 
R

Robert Klemme

2010/2/8 Robert Klemme said:
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/
 
B

Brian Candler

Mikkel said:
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)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top