Possible patch for singleton.rb

Hello all,

As you know, if you mix-in Singleton module in a class,
#new is made private, and only one instance of the class
may be created via #instance method - the first time it
is called, #new is called from within a class, all following
calls of #instance will return the already created object.

What is peculiar is that you cannot pass any arguments
to the constructor when creating the object, that is, you
cannot do

a = SomeClass.instance(1,2,3)

But it is trivial to enable it, here is the diff of singleton.rb:

18c18
< def instance

···
  def instance(*args)

22c22
< @instance ||= new

  @__instance__ ||= new(*args)

Naturally, all non-first arguments of #instance will be ignored.

Happy Rubying,
Yuri Leikind

leikind@mova.org (Yuri Leikind) writes:

What is peculiar is that you cannot pass any arguments
to the constructor when creating the object, that is, you
cannot do

a = SomeClass.instance(1,2,3)

But it is trivial to enable it, here is the diff of singleton.rb:

Except…

How can a singleton have arguments to ‘instance’ when the underlying
object is constructed only once? You get a kind of ‘first one in wins’
situation:

a = SomeClass.instance(1, 2, 3)

# ....

b = SomeClass.instance('a, 'b', 'c')

# ... Much confusion and gnashing of teeth

Perhaps a better approach might be to have a call that sets the
parameters to be used when the object is created:

SomeClass.set_init_args(1, 2, 3)

a = SomeClass.instance    -- will do   new(1, 2, 3)

b = SomeClass.instance    -- will reuse existing object

It would be a runtime error to call set_init_args more than once or
call it after the object has been created.

Cheers

Dave

Hi,

···

In message “possible patch for singleton.rb” on 02/10/10, Yuri Leikind leikind@mova.org writes:

What is peculiar is that you cannot pass any arguments
to the constructor when creating the object, that is, you
cannot do

a = SomeClass.instance(1,2,3)

But it is trivial to enable it, here is the diff of singleton.rb:

But you will not be sure if the arguments are used or not.
I think it’s bad.

						matz.

“Dave Thomas” Dave@PragmaticProgrammer.com wrote in message
news:m265wao0b1.fsf@zip.local.thomases.com

leikind@mova.org (Yuri Leikind) writes:

SomeClass.set_init_args(1, 2, 3)

I agree, I was about to post the same.

a = SomeClass.instance    -- will do   new(1, 2, 3)

b = SomeClass.instance    -- will reuse existing object

It would be a runtime error to call set_init_args more than once or
call it after the object has been created.

Or it could overwrite the init_args at the the discretion of the class.
has_init_args? could be used to test.

I think it shouldn’t be a language feature, but a feature of the class being
implemented.

Mikkel

Why would it be a runtime error to call set_init_args more than
once? After the object has been created, absolutely, but why not let
the initial values mutate as necessary until creation?

-austin
– Austin Ziegler, austin@halostatue.ca on 2002.10.10 at 10.37.53

···

On Thu, 10 Oct 2002 23:06:44 +0900, Dave Thomas wrote:

Perhaps a better approach might be to have a call that sets the
parameters to be used when the object is created:

SomeClass.set_init_args(1, 2, 3)

a = SomeClass.instance # will do new(1, 2, 3)

b = SomeClass.instance # will reuse existing object

It would be a runtime error to call set_init_args more than once
or call it after the object has been created.

“Dave Thomas” wrote

Except…

How can a singleton have arguments to ‘instance’ when the underlying
object is constructed only once? You get a kind of ‘first one in wins’
situation:

a = SomeClass.instance(1, 2, 3)

# ....

b = SomeClass.instance('a, 'b', 'c')

# ... Much confusion and gnashing of teeth

agreed

Perhaps a better approach might be to have a call that sets the
parameters to be used when the object is created:

SomeClass.set_init_args(1, 2, 3)

a = SomeClass.instance    -- will do   new(1, 2, 3)

b = SomeClass.instance    -- will reuse existing object

It would be a runtime error to call set_init_args more than once or
call it after the object has been created.

What would you if one thread is busy calling
set_init_args(1, 2, 3)'' and while another would be calling the instance’ method. It seems to
me that you need to distribute the instancation logic'' over two methods (set_init_args and instance). Personally I doubt that it is really worth the trouble. In the most cases it is probably enough to modify initialize’’ in such way that ``new’’ picks up
the correct initializing values …

/Christoph

“Dave Thomas” Dave@PragmaticProgrammer.com wrote in message
news:m265wao0b1.fsf@zip.local.thomases.com

leikind@mova.org (Yuri Leikind) writes:

What is peculiar is that you cannot pass any arguments
to the constructor when creating the object, that is, you
cannot do

a = SomeClass.instance(1,2,3)

But it is trivial to enable it, here is the diff of singleton.rb:

Except…

How can a singleton have arguments to ‘instance’ when the underlying
object is constructed only once? You get a kind of ‘first one in wins’
situation:

a = SomeClass.instance(1, 2, 3)

# ....

b = SomeClass.instance('a, 'b', 'c')

# ... Much confusion and gnashing of teeth

Perhaps a better approach might be to have a call that sets the
parameters to be used when the object is created:

SomeClass.set_init_args(1, 2, 3)

a = SomeClass.instance    -- will do   new(1, 2, 3)

b = SomeClass.instance    -- will reuse existing object

It would be a runtime error to call set_init_args more than once or
call it after the object has been created.

This approach is perhaps wiser than mine. The only thing is
that we are complicating Singleton by adding a class method

Best regards,
Yuri Leikind

Hello Yuri,

Friday, October 11, 2002, 2:56:27 PM, you wrote:

a = SomeClass.instance(1,2,3)
SomeClass.set_init_args(1, 2, 3)

This approach is perhaps wiser than mine. The only thing is
that we are complicating Singleton by adding a class method

may be simpler approach will be:

class Singleton
@@a = false
def initialize(*a)
raise if @@a
@@a=true
super(*a)
end

end

?

···


Best regards,
Bulat mailto:bulatz@integ.ru