[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Class.new ?

Warren Seltzer

1/25/2007 4:33:00 PM

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


Fred



9 Answers

Avdi Grimm

1/25/2007 4:42:00 PM

0

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

On 1/25/07, Warren Seltzer <warrens@actcom.net.il> wrote:
> Thanks, I see how to make it work, but I don't see need to ever do it...
>
>
> Fred
>
>
>
>

WoNáDo

1/25/2007 4:47:00 PM

0

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

1/25/2007 5:02:00 PM

0

2007/1/25, Avdi Grimm <avdi@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

1/25/2007 5:03:00 PM

0

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 Gray

1/25/2007 5:30:00 PM

0

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_thre...
8512e7e5395f50a2/363e0fc501d33516?lnk=raot#363e0fc501d33516

James Edward Gray II

David Chelimsky

1/25/2007 5:37:00 PM

0

On 1/25/07, Warren Seltzer <warrens@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

1/25/2007 5:38:00 PM

0

On 25.01.2007 18:30, James Edward Gray II wrote:
> 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#363e0f...

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

1/25/2007 5:40:00 PM

0

On 25.01.2007 18:02, Alex Young wrote:
> 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 :-)

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

1/25/2007 5:48:00 PM

0

Robert Klemme wrote:
> On 25.01.2007 18:02, Alex Young wrote:
>> 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 :-)
>
> 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?
Not really - just wondering what it would look like :-) I might have a
play later, and see if it makes any sense.

--
Alex