不能“require()”某些核心包?

时间:2014-01-27 23:57:30

标签: ruby

看来,Ruby正在和我搞砸。那还是RubyMine。所以,我创建了一个名为NetworkDaemon的类,它是NetworkDaemonSpecification中的相关规范。很简单。

但是,我的问题是我似乎无法使用require 'tcp_server'中的network_daemon.rb,但我可以network_daemon_specification.rb

如果我尝试引用tcp_server中的network_daemon.rb(在network_daemon_specification.rb中进行了单元测试,则会得到以下stacktrace

E:/Ruby200/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:53:in `require': cannot load such file -- tcp_server (LoadError)
    from E:/Ruby200/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:53:in `require'
    from E:/Ruby/RubyCraft/network/network_daemon.rb:1:in `<top (required)>'
    from E:/Ruby200/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:53:in `require'
    from E:/Ruby200/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:53:in `require'
    from E:/Ruby/RubyCraft/specification/network_daemon_specification.rb:1:in `<top (required)>'
    from -e:1:in `load'
    from -e:1:in `<main>'

但是,我可以在tcp_server中引用network_daemon_specification就好了。这是怎么回事? d:

network_daemon.rb

require 'tcp_server'

module RubyCraft
  module Network
    # The network daemon controls the listening and responses
    class NetworkDaemon
      attr_reader :port
      # Creates the NetworkDaemon from the given port
      # @param [Fixnum] port
      def initialize port
        @port = port
        @sock = TCPServer.new @port
      end

      # Closes the NetworkDaemon
      def close
        @sock.close
      end
    end
  end
end

network_daemon_specification.rb

# Unit tests for the Network Daemon that will be used to listen for connections
module RubyCraft
  module Specifications
    class NetworkDaemonSpecification < Test::Unit::TestCase
      def setup
        @port = 9999
        @daemon = NetworkDaemon.new @port
      end

      def test_port_should_equal_injected_port
        assert_equal @daemon.port, @port
      end

      def teardown
        @daemon.close
      end
    end
  end
end

2 个答案:

答案 0 :(得分:0)

结果tcp_server不是你需要的(尽管RubyMine说的是) - 你应该要求socket

答案 1 :(得分:0)

您需要require 'socket'而不是tcp_server

查看TCPServer文档以获取更多信息http://www.ruby-doc.org/stdlib-1.9.3/libdoc/socket/rdoc/TCPServer.html