D41d8cd98f00b204e9800998ecf8427e

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?

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 !

6e36a31a1dc5d87c1bf028e5b3816dbe

v0n

August 3, 2011, August 03, 2011 07:29, permalink

No rating. Login to rate!

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
D41d8cd98f00b204e9800998ecf8427e

moshee

August 3, 2011, August 03, 2011 07:36, permalink

No rating. Login to rate!

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
D41d8cd98f00b204e9800998ecf8427e

moshee

August 3, 2011, August 03, 2011 07:37, permalink

No rating. Login to rate!

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
D41d8cd98f00b204e9800998ecf8427e

moshee

August 3, 2011, August 03, 2011 07:37, permalink

No rating. Login to rate!

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
6e36a31a1dc5d87c1bf028e5b3816dbe

v0n

August 3, 2011, August 03, 2011 07:42, permalink

No rating. Login to rate!

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

Your refactoring





Format Copy from initial code

or Cancel