class IRC < TCPSocket
def cmd str
send "#{str}\r\n", 0
end
end
class Commander
def initialize
@irc = IRC.new
end
def test
@irc.cmd 'PRIVMSG #ruby :help!'
end
end
Refactorings
No refactoring yet !
v0n
August 3, 2011, August 03, 2011 07:29, permalink
Does cmd need to be an instance method? Could it be a class method? In that case the following should work.
class IRC < TCPSocket
def self.cmd str
send "#{str}\r\n", 0
end
end
class Commander
def test
IRC.cmd 'PRIVMSG #ruby :help!'
end
end
moshee
August 3, 2011, August 03, 2011 07:36, permalink
cmd should be an instance method since I'm making an IRC.new for the socket connection. I was hoping to do something like this:
# ...
class Commander
def test
cmd 'PRIVMSG #ruby :help!'
end
end
moshee
August 3, 2011, August 03, 2011 07:37, permalink
cmd should be an instance method since I'm making an IRC.new for the socket connection. I was hoping to do something like the following. I suppose restructuring the IRC class would be fine.
# ...
class Commander
def test
cmd 'PRIVMSG #ruby :help!'
end
end
moshee
August 3, 2011, August 03, 2011 07:37, permalink
cmd should be an instance method since I'm making an IRC.new for the socket connection. I was hoping to do something like the following. I suppose restructuring the IRC class would be fine.
# ...
class Commander
def test
cmd 'PRIVMSG #ruby :help!'
end
end
v0n
August 3, 2011, August 03, 2011 07:42, permalink
So in that case you'll need something like that. if you really want to 'hide' the @irc.cmd, you could put it in a private method.
class IRC < TCPSocket
def cmd str
send "#{str}\r\n", 0
end
end
class Commander
def initialize
@irc = IRC.new
end
def test
cmd 'PRIVMSG #ruby :help!'
end
private
def cmd str
@irc.cmd str
end
end
Is there any way to call cmd from Commander#test without using '@irc.cmd' like it is in the code, and without combining the two classes IRC and Commander?