[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Access variables in singleton?

Steve V

4/8/2005 10:30:00 PM

I created the following small singleton class, but it seems that my
variables are ignored.

require 'singleton'

class Context
include Singleton

@companyname = "acme"

def company()
return @companyname
end

def change(name)
@companyname = name
end

end

If I set the @companyname variable within the company function, and then
return it, the value is there. After that though, it seems that the variable
just disappears. What am I doing wrong?

Thanks,
Steve




1 Answer

Ben Giddings

4/8/2005 10:42:00 PM

0

On Friday 08 April 2005 18:30, Steve V wrote:
> I created the following small singleton class, but it seems that my
> variables are ignored.
>
> require 'singleton'
>
> class Context
> include Singleton
>
> @companyname = "acme"

Wrong context, this is defined within the "Context" class definition, not
within the one object (instance of Context) that you create. What you
want is:

def initialize
@companyname = "acme"
end

Singleton will make "new" private, but you still want to use "initialize"
which will be called by "instance"

Ben