# String constant reference to another class instance variable

**URL:** <https://www.ruby-forum.com/t/string-constant-reference-to-another-class-instance-variable/183119>\
**Category:** Ruby\
**Created:** [February 8, 2010, 2:36pm UTC](https://www.ruby-forum.com/t/string-constant-reference-to-another-class-instance-variable/183119 "2010-02-08T14:36:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![nginxchina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f0a364/32.png) [@nginxchina](https://www.ruby-forum.com/u/nginxchina)\
**Post date:** [February 8, 2010, 2:36pm UTC](https://www.ruby-forum.com/t/string-constant-reference-to-another-class-instance-variable/183119/1 "2010-02-08T14:36:20Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![nginxchina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f0a364/32.png) [@nginxchina](https://www.ruby-forum.com/u/nginxchina)\
**Post date:** [February 8, 2010, 3:17pm UTC](https://www.ruby-forum.com/t/string-constant-reference-to-another-class-instance-variable/183119/2 "2010-02-08T15:17:26Z")

</div>

2010/2/8 Mikkel K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> @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

---

<div class="post-metadata">

**Author:** ![nginxchina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f0a364/32.png) [@nginxchina](https://www.ruby-forum.com/u/nginxchina)\
**Post date:** [February 8, 2010, 3:47pm UTC](https://www.ruby-forum.com/t/string-constant-reference-to-another-class-instance-variable/183119/3 "2010-02-08T15:47:03Z")

</div>

class String  
attr\_accessor :socket  
end

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

---

<div class="post-metadata">

**Author:** ![nginxchina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f0a364/32.png) [@nginxchina](https://www.ruby-forum.com/u/nginxchina)\
**Post date:** [February 8, 2010, 3:59pm UTC](https://www.ruby-forum.com/t/string-constant-reference-to-another-class-instance-variable/183119/4 "2010-02-08T15:59:23Z")

</div>

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](http://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…

---

<div class="post-metadata">

**Author:** ![nginxchina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f0a364/32.png) [@nginxchina](https://www.ruby-forum.com/u/nginxchina)\
**Post date:** [February 8, 2010, 4:39pm UTC](https://www.ruby-forum.com/t/string-constant-reference-to-another-class-instance-variable/183119/5 "2010-02-08T16:39:59Z")

</div>

2010/2/8 Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 2010/2/8 Mikkel K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > 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

---

<div class="post-metadata">

**Author:** ![nginxchina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f0a364/32.png) [@nginxchina](https://www.ruby-forum.com/u/nginxchina)\
**Post date:** [February 8, 2010, 4:31pm UTC](https://www.ruby-forum.com/t/string-constant-reference-to-another-class-instance-variable/183119/6 "2010-02-08T16:31:12Z")

</div>

2010/2/8 Mikkel K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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

---

<div class="post-metadata">

**Author:** ![nginxchina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f0a364/32.png) [@nginxchina](https://www.ruby-forum.com/u/nginxchina)\
**Post date:** [February 8, 2010, 6:56pm UTC](https://www.ruby-forum.com/t/string-constant-reference-to-another-class-instance-variable/183119/7 "2010-02-08T18:56:44Z")

</div>

Mikkel K. wrote:

> IRC::Connect hostname: ‘[irc.phora.net](http://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)
