Class.new?

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

Warren S. schrieb:

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

Thanks, I see how to make it work, but I don’t see need to ever do it…

Fred

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

2007/1/25, Avdi G. [email protected]:

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…

Warren S. 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

On Jan 25, 2007, at 11:01 AM, Robert K. 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 G. cross-
check turned up nothing:

http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/
8512e7e5395f50a2/363e0fc501d33516?lnk=raot#363e0fc501d33516

James Edward G. II

On 1/25/07, Warren S. [email protected] 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).

Avdi G. 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 :slight_smile:

On 25.01.2007 18:30, James Edward G. II wrote:

On Jan 25, 2007, at 11:01 AM, Robert K. 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 G. cross-check
turned up nothing:

http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/8512e7e5395f50a2/363e0fc501d33516?lnk=raot#363e0fc501d33516

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 K. wrote:

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 :slight_smile: I might have a
play later, and see if it makes any sense.

On 25.01.2007 18:02, Alex Y. wrote:

Avdi G. 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 :slight_smile:

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs