hemant
2006-Oct-27 21:10 UTC
[Eventmachine-talk] call EventMachine.connect from initialize
Trying to call, EventMachine::conect from initialize method of a class
never works.
Here, I am getting exception:
acts_as_client.rb:44:in `initialize'': wrong number of arguments (0 for
1) (ArgumentError)
from acts_as_client.rb:44:in `initialize''
from acts_as_client.rb:77:in `initialize''
from
/usr/lib/ruby/gems/1.8/gems/eventmachine-0.7.0/lib/eventmachine.rb:718:in
`event_callback''
from
/usr/lib/ruby/gems/1.8/gems/eventmachine-0.7.0/lib/eventmachine.rb:201:in
`run''
from acts_as_client.rb:76:in `initialize''
from acts_as_client.rb:82
However, If i move EventMachine.connect to another class method , it
works, but that way...meta programming hooks throw up errors, like:
set_instance_variables undefined...
Any solution?
require ''rubygems''
require ''eventmachine''
class MyProtocolHandler < EventMachine::Connection
def parse_foobar data
data.chomp!
@game << data
send_data (@game+"\n")
end
def post_init
p "New connection made"
end
def self.metaclass; class << self; self; end; end;
def self.traits( *args )
return @traits if args.empty?
attr_accessor *args
args.each do |a|
metaclass.instance_eval do
define_method( a ) do |val|
@traits ||= {}
@traits[a] = val
end
end
end
# just inheriting this stupid thing will enable call to initialize method
class_eval do
define_method( :initialize ) do|*arry|
super *arry
self.class.traits.each do |k,v|
instance_variable_set("@#{k}",v)
end
EventMachine.connect("localhost",3901,self)
end
end
# method that defines the receive_data
class_eval do
define_method( :receive_data ) do |data|
self.send("parse_#{@format}",data)
end
end
end # end of self.traits method
traits :life,:game,:format
end # class MyProtocolHandler
class FooHandler < MyProtocolHandler
life 100
game ""
format ''foobar''
end
class Base
# will have data_client sockets
# some other foobar
def initialize
EventMachine.run {
bar = FooHandler.new
}
end
end
base = Base.new
--
There was only one Road; that it was like a great river: its springs
were at every doorstep, and every path was its tributary.
Francis Cianfrocca
2006-Oct-27 22:37 UTC
[Eventmachine-talk] call EventMachine.connect from initialize
On 10/28/06, hemant <gethemant at gmail.com> wrote:> > Trying to call, EventMachine::conect from initialize method of a class > never works. > > > # just inheriting this stupid thing will enable call to initialize > method > class_eval do > define_method( :initialize ) do|*arry| > super *arry > self.class.traits.each do |k,v| > instance_variable_set("@#{k}",v) > end > EventMachine.connect("localhost",3901,self) > end > > endI''m having trouble following all this metaprogramming. I assume you defined MyProtocolHandler#initialize inside a class_eval so that self would refer to the class, in the EventMachine#connect call. I''ll bet this is causing some kind of problem. Remember, #connect creates a new instance of the class that is given as its third argument. So I don''t see how this could work without causing an infinite regress. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/eventmachine-talk/attachments/20061028/14823712/attachment.html
hemant
2006-Oct-28 00:19 UTC
[Eventmachine-talk] call EventMachine.connect from initialize
On 10/28/06, Francis Cianfrocca <garbagecat10 at gmail.com> wrote:> On 10/28/06, hemant <gethemant at gmail.com> wrote: > > Trying to call, EventMachine::conect from initialize method of a class > > never works. > > > > > > # just inheriting this stupid thing will enable call to initialize > method > > class_eval do > > define_method( :initialize ) do|*arry| > > super *arry > > self.class.traits.each do |k,v| > > instance_variable_set("@#{k}",v) > > end > > EventMachine.connect("localhost",3901,self) > > end > > > > end > > > > I''m having trouble following all this metaprogramming. I assume you defined > MyProtocolHandler#initialize inside a class_eval so that self would refer to > the class, in the EventMachine#connect call. I''ll bet this is causing some > kind of problem. Remember, #connect creates a new instance of the class that > is given as its third argument. So I don''t see how this could work without > causing an infinite regress.Ok..calling connect from initialize would never work out...so i did a small workaround and move that to a class method and whoever wants an instance of the class of the would call that method. Working pretty fine now. Basically, I wanted to write a higher level class, that can act as a server and client both and hooks up proper parsers based on attributes dynamically. -- There was only one Road; that it was like a great river: its springs were at every doorstep, and every path was its tributary.