Google 網路論壇不再支援新的 Usenet 貼文或訂閱項目,但過往內容仍可供查看。

Class.new ?

瀏覽次數:0 次
跳到第一則未讀訊息

Warren Seltzer

未讀,
2007年1月25日 上午8:09:512007/1/25
收件者:
The interpreter lets me make this mistake:

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


Wolfgang Nádasi-Donner

未讀,
2007年1月25日 上午9:10:322007/1/25
收件者:
Warren Seltzer schrieb:

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

Warren Seltzer

未讀,
2007年1月25日 上午11:33:262007/1/25
收件者:
Thanks, I see how to make it work, but I don't see need to ever do it...


Fred

Avdi Grimm

未讀,
2007年1月25日 上午11:41:322007/1/25
收件者:
The first example off the top of my head:

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

Wolfgang Nádasi-Donner

未讀,
2007年1月25日 上午11:47:022007/1/25
收件者:
Warren Seltzer schrieb:

> Thanks, I see how to make it work, but I don't see need to ever do it...

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

Robert Klemme

未讀,
2007年1月25日 中午12:01:552007/1/25
收件者:
2007/1/25, Avdi Grimm <av...@avdi.org>:

> The first example off the top of my head:
>
> 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.

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 Young

未讀,
2007年1月25日 中午12:02:572007/1/25
收件者:
Avdi Grimm wrote:
> The first example off the top of my head:
>
> A class which might return an existing object out of an object pool,
> instead of returning a new object.
Ooh... Has anyone written a ThreadPool which works that way? That
would be nice :-)

--
Alex

James Edward Gray II

未讀,
2007年1月25日 中午12:30:162007/1/25
收件者:
On Jan 25, 2007, at 11:01 AM, Robert Klemme wrote:

> 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

David Chelimsky

未讀,
2007年1月25日 中午12:37:152007/1/25
收件者:
On 1/25/07, Warren Seltzer <war...@actcom.net.il> wrote:
> Thanks, I see how to make it work, but I don't see need to ever do it...

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).

Robert Klemme

未讀,
2007年1月25日 中午12:38:172007/1/25
收件者:

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

Robert Klemme

未讀,
2007年1月25日 中午12:40:282007/1/25
收件者:

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 Young

未讀,
2007年1月25日 中午12:47:312007/1/25
收件者:
Not really - just wondering what it would look like :-) I might have a
play later, and see if it makes any sense.

--
Alex

0 則新訊息