class Act
def Act.new
# Anything, really
end
end
######
Coding your own "new" just seems to screw things up. What is it good for and why isn't it a
syntax error?
Fred
You can write your own "new" method if it makes sense for your application. As I
understood it correctly, the initialization process uses three methods: "new",
"allocate", and "initialize".
>>>>> Code >>>>>
class Otto
def self.new(*p,&b)
puts "Here do something special..."
super(*p,&b)
end
def hi
puts "'Hi!' from an 'Otto' instance"
end
end
p Otto.new
Otto.new.hi
>>>>> Output >>>>>
Here do something special...
#<Otto:0x2aeb148>
Here do something special...
'Hi!' from an 'Otto' instance
>>>>> EoE >>>>>
Wolfgang Nádasi-Donner
Fred
A class which might return an existing object out of an object pool,
instead of returning a new object.
But there are plenty of other possibilities.
--
Avdi
I think it is only necessary, if you need a special "allocate" method for your
application. I can imagine that this can happen when there is a need to store
objects somewhere outside over a network or so.
Wolfgang Nádasi-Donner
Singletons for example.
irb(main):001:0> require 'singleton'
=> true
irb(main):002:0> class Foo
irb(main):003:1> include Singleton
irb(main):004:1> end
=> Foo
irb(main):005:0> Foo.new
NoMethodError: private method `new' called for Foo:Class
from (irb):5
from :0
irb(main):006:0> Foo.instance
=> #<Foo:0x7ef71114>
irb(main):007:0> Foo.instance
=> #<Foo:0x7ef71114>
irb(main):008:0> Foo.instance
=> #<Foo:0x7ef71114>
irb(main):009:0>
Kind regards
robert
PS: The gateway seems to have trouble again - I see only some of the
postings in this thread on the news side...
--
Alex
> PS: The gateway seems to have trouble again - I see only some of the
> postings in this thread on the news side...
Which messages are missing? My simple inbox/Google Groups cross-
check turned up nothing:
http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/
8512e7e5395f50a2/363e0fc501d33516?lnk=raot#363e0fc501d33516
James Edward Gray II
I stub #new all the time when testing Rails applications:
thing = mock("thing")
Thing.stub!(:new).and_return(thing)
This is extraordinarily useful when trying to isolate components for
testing, and I can only do this if the mock framework can override new
(which it does in this case).
Hm, that's weird. Checking again I see all the postings. Some where
marked as read although I believe I didn't mark / read them myself.
So either it's a client issue or a stupid-me issue. I am sorry for the
noise.
Thanks for checking anyway!
robert
I don't think this is the right pattern for a thread pool. There you
usually start all the threads and let them fetch tasks from a single
queue. Taking threads out of a pool and into a pool seems much more
complex especially since you want to block inactive threads. The queue
variant is much simpler. Or did you have something else in mind?
Kind regards
robert
--
Alex