Class.new ?

W

Warren Seltzer

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
 
W

Wolfgang Nádasi-Donner

Warren said:
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

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

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

Here do something special...
#<Otto:0x2aeb148>
Here do something special...
'Hi!' from an 'Otto' instance

Wolfgang Nádasi-Donner
 
A

Avdi Grimm

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

Robert Klemme

2007/1/25 said:
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...
 
A

Alex Young

Avdi said:
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 :)
 
D

David Chelimsky

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

Robert Klemme

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
 
A

Alex Young

Robert said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,268
Latest member
AshliMacin

Latest Threads

Top