Initialize *args weirdness

2 views
Skip to first unread message

Kenny Stone

unread,
Nov 29, 2009, 5:02:17 PM11/29/09
to eventm...@googlegroups.com
I don't understand why the first arg is seeming getting removed when I inherit from EM::Connection


require 'eventmachine'

module Dude

  class Adapter < EM::Connection
    def initialize(*args)
      puts ">> #{self.class}: " << args.inspect
      super(args)
    end
  end

  class AdapterNoEM
    def initialize(*args)
      puts ">> #{self.class}: " << args.inspect
    end
  end
end


Dude::Adapter.new( 'arg1', 'arg2', arg: '3' )
Dude::AdapterNoEM.new( 'arg1', 'arg2', arg: '3' )

===>

>> Dude::Adapter: ["arg2", {:arg=>"3"}]
>> Dude::AdapterNoEM: ["arg1", "arg2", {:arg=>"3"}]

help?  where is it going?  what should it be?

Jake Douglas

unread,
Nov 29, 2009, 5:37:24 PM11/29/09
to eventm...@googlegroups.com
Hi Kenny,

You don't need to call super from your initialize method. EM::Connection.new will take whatever basic arguments are required by it and pass the rest on to your initialize method.

For example:

EM.connect("127.0.0.1", 12345, SomeHandler, :foo, :bar)

will end up calling SomeHandler#initialize with :foo and :bar


--

You received this message because you are subscribed to the Google Groups "EventMachine" group.
To post to this group, send email to eventm...@googlegroups.com.
To unsubscribe from this group, send email to eventmachine...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/eventmachine?hl=en.



--
Jake Douglas
206-795-9207

Kenny Stone

unread,
Nov 29, 2009, 6:30:11 PM11/29/09
to eventm...@googlegroups.com
Thanks Jake, that's good to know; I was just following the general introduction, which has the 'super' call.

I still see "args" getting the first element removed, though, which I don't understand.

Kenny Stone

unread,
Nov 29, 2009, 7:44:16 PM11/29/09
to eventm...@googlegroups.com
The reason I ask is for unit tests, where I will never call "EM::Connect"

adapter = Adapter.new( arg1, arg2, arg3, ... )
adapter.receive_data( my_data )
assert something_happened_with_my_data

Is there a better pattern you guys use for unit tests?

Jake Douglas

unread,
Nov 29, 2009, 7:47:21 PM11/29/09
to eventm...@googlegroups.com
Now I understand what you mean. .new expects the first argument to be a connection 'signature', assigned to it by the reactor. You can probably just pass nil there for your unit tests.

Other folks may have better feedback in regards to methods of testing your connection handlers.

Kenny Stone

unread,
Nov 29, 2009, 7:54:57 PM11/29/09
to eventm...@googlegroups.com
I was making the (poor) assumption that 'new' calls 'initialize' without anything else happening to the argument list.  Thanks!

Jake Douglas

unread,
Nov 29, 2009, 8:00:21 PM11/29/09
to eventm...@googlegroups.com
It isn't very intuitive and most people either had to figure it out by asking like you or don't know about it at all. We should make it more clear somewhere in the examples.

Kenny Stone

unread,
Nov 29, 2009, 8:26:04 PM11/29/09
to eventm...@googlegroups.com
It doesn't seem as though calling "new" is the way to go for unit testing.  

"post_init" is getting called at some point after initialize although I'm never calling "Connect".  The documentation says post_init is called by the event loop immediately after the network connection has been established, but no network connection is happening here.  Is "new" only supposed to be called by the reactor?  It seems like some assumptions are made when "new" is called.


Here's the simple example of "new" eventually making the call to post_init even though there was never a network connection:
---

require 'eventmachine'

module Dude

  class Adapter < EM::Connection
    def initialize(*args)
      puts "inititialize"
    end
    def post_init
      puts "post_init"
    end
  end

end

Dude::Adapter.new(nil)

==> ruby my_adapter.rb

inititialize
post_init

---

And checking 'error?' doesn't work, either.

---

module Dude

  class Adapter < EM::Connection
    def initialize(*args)
      puts "inititialize"
    end
    def post_init
      puts "post_init"
      send_data "login_msg" unless error?
    end
  end

end

Dude::Adapter.new(nil)

==> ruby my_adapter.rb

inititialize
post_init
/usr/local/lib/ruby/gems/1.9.1/gems/eventmachine-0.12.10/lib/em/connection.rb:233:in `report_connection_error_status': no implicit conversion from nil to integer (TypeError)
from /usr/local/lib/ruby/gems/1.9.1/gems/eventmachine-0.12.10/lib/em/connection.rb:233:in `error?'
from try.rb:11:in `post_init'
from /usr/local/lib/ruby/gems/1.9.1/gems/eventmachine-0.12.10/lib/em/connection.rb:45:in `block in new'
from /usr/local/lib/ruby/gems/1.9.1/gems/eventmachine-0.12.10/lib/em/connection.rb:36:in `instance_eval'
from /usr/local/lib/ruby/gems/1.9.1/gems/eventmachine-0.12.10/lib/em/connection.rb:36:in `new'
from try.rb:16:in `<main>'


Reply all
Reply to author
Forward
0 new messages