Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TCPSocket.openも書き換えたほうがいい #2

edvakf opened this issue Jan 20, 2013 · 0 comments


Copy link

@edvakf edvakf commented Jan 20, 2013

#1 対応ありがとうございます。

関連してもう一つお願いしたいことがあります。 は親の親の親のクラスの にたどり着くのですが、rb_class_new_instance っていうのでインスタンスが作られるので new メソッドを通らないっぽいです。

static VALUE
rb_io_s_open(int argc, VALUE *argv, VALUE klass)
    VALUE io = rb_class_new_instance(argc, argv, klass);

    if (rb_block_given_p()) {
        return rb_ensure(rb_yield, io, io_close, io);

    return io;

open もモンキーパッチしました。

diff --git a/lib/unix_socket_hack.rb b/lib/unix_socket_hack.rb
index f38b35b..51e913b 100644
--- a/lib/unix_socket_hack.rb
+++ b/lib/unix_socket_hack.rb
@@ -11,6 +11,7 @@ class UNIXSocketHack
   def self.apply(mapping)
     TCPSocket.singleton_class.class_eval do
       alias_method :new_without_unixsockhack, :new
+      alias_method :new_without_unixsockhack, :open

       define_method(:new_with_unixsockhack) do |remote_host, remote_port, local_host=nil, local_port=nil|
         if val = mapping[[remote_host, remote_port].join(":")]
@@ -29,6 +30,7 @@ class UNIXSocketHack

       alias_method :new, :new_with_unixsockhack
+      alias_method :open, :new_with_unixsockhack

     UNIXSocket.class_eval do
diff --git a/spec/unix_socket_hack_spec.rb b/spec/unix_socket_hack_spec.rb
index e75813c..33fd163 100644
--- a/spec/unix_socket_hack_spec.rb
+++ b/spec/unix_socket_hack_spec.rb
@@ -20,15 +20,21 @@ describe "UnixSocketHack" do
   it "connect to unixsocket:9999 should be UNIXSock" do 
     sock ='localhost', 9999);
     sock.class.should == UNIXSocket
+    sock ='localhost', 9999);
+    sock.class.should == UNIXSocket

   it "connect to localhost:9998 should be TCPSocket" do 
     sock ='localhost', 9998);
     sock.class.should == TCPSocket
+    sock ='localhost', 9998);
+    sock.class.should == TCPSocket

   it "connect to should be TCPSocket" do 
     sock ='', 9999);
     sock.class.should == TCPSocket
+    sock ='', 9999);
+    sock.class.should == TCPSocket

具体的な例では、Net::HTTP が を使っているので、それを UNIXSocket を使うようにしたいです。

Ruby の lib/net/http.rb

    def connect
      if proxy? then
        conn_address = proxy_address
        conn_port    = proxy_port
        conn_address = address
        conn_port    = port

      D "opening connection to #{conn_address}:#{conn_port}..."
      s = Timeout.timeout(@open_timeout, Net::OpenTimeout) {, conn_port, @local_host, @local_port)
      D "opened"
@walf443 walf443 closed this in 00b4880 Jan 21, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
2 participants
You can’t perform that action at this time.