# Need help with singleton!

**URL:** <https://www.ruby-forum.com/t/need-help-with-singleton/132314>\
**Category:** Ruby\
**Created:** [March 19, 2008, 4:22am UTC](https://www.ruby-forum.com/t/need-help-with-singleton/132314 "2008-03-19T04:22:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![di3go](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a9a28c/32.png) [@di3go](https://www.ruby-forum.com/u/di3go)\
**Post date:** [March 19, 2008, 4:22am UTC](https://www.ruby-forum.com/t/need-help-with-singleton/132314/1 "2008-03-19T04:22:35Z")

</div>

Hi 🙂

Yea, again a question to help me building the game XD  
I have a class called Mob, when i instance a mob from the main i search  
the name of mob in a list, if the name is there i create it and i call a  
singleton class with all the images of the mob, so if i create the same  
kind of mob again he will create it and use the images from the  
singleton, to save memory.  
Well here comes the problem, if all mobs die and i go town and i never  
see this mob again, the singleton gonna still in the memory? 1 mob isnt  
trouble, just 2mb, but if i see 200 kinds of mob its alot of memory and  
i need free this, i tried to set the singleton nil but when i create the  
same kind of mob again gives me a error.

So have any way to create the mobs and when all the mobs die, delete the  
singleton too? And ofcourse, the same mob can be instanciable after if  
the player see it.

And how i can destroy objetos is just = nil it?

Thanks guys! 🙂

---

<div class="post-metadata">

**Author:** ![di3go](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a9a28c/32.png) [@di3go](https://www.ruby-forum.com/u/di3go)\
**Post date:** [March 19, 2008, 7:19am UTC](https://www.ruby-forum.com/t/need-help-with-singleton/132314/2 "2008-03-19T07:19:15Z")

</div>

Hi,

If you’re using the Singleton Module from the standard library, i  
think it must have a flag or something that automatically gets marked  
when it first creates the instance object. By setting that instance to  
nil, you allow the garbage collector to clean it, however, the flag  
being a class member, exists in memory still and remembers that it has  
already created the instance. So, allows you to access it, which in  
effect passes you a nil, hence the error.

Solution… hmm… probably you’d like to use a self-brewed singleton  
class. Or by adding a resource-cleanup method, that releases all your  
images, that can be called by the last mob’s destructor. I’d go with  
the second, so that your singleton class becomes lean, yet still  
exists, when you don’t need it.

Mt.

---

<div class="post-metadata">

**Author:** ![di3go](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a9a28c/32.png) [@di3go](https://www.ruby-forum.com/u/di3go)\
**Post date:** [March 19, 2008, 1:06pm UTC](https://www.ruby-forum.com/t/need-help-with-singleton/132314/3 "2008-03-19T13:06:43Z")

</div>

mutahhir hayat wrote:

> Hi,
> 
> If you’re using the Singleton Module from the standard library, i  
> think it must have a flag or something that automatically gets marked  
> when it first creates the instance object. By setting that instance to  
> nil, you allow the garbage collector to clean it, however, the flag  
> being a class member, exists in memory still and remembers that it has  
> already created the instance. So, allows you to access it, which in  
> effect passes you a nil, hence the error.
> 
> Solution… hmm… probably you’d like to use a self-brewed singleton  
> class. Or by adding a resource-cleanup method, that releases all your  
> images, that can be called by the last mob’s destructor. I’d go with  
> the second, so that your singleton class becomes lean, yet still  
> exists, when you don’t need it.
> 
> Mt.

Thanks 🙂

i did this

private\_class\_method :new  
attr\_accessor :animacao, :animacao\_delay, :deslocamento\_x,  
:deslocamento\_y

def Info.create(\*args, &block)  
@me = new(\*args, &block) if ! @me

```
if @descarregar == true
  @me.initialize(*args, &block)
else
  return @me
end

```

end  
def initialize()  
@descarregar = false  
end  
def destruir  
everything = nil  
@descarregar = true  
end

its working 🙂

Thanks mutahhir

---

<div class="post-metadata">

**Author:** ![di3go](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a9a28c/32.png) [@di3go](https://www.ruby-forum.com/u/di3go)\
**Post date:** [March 19, 2008, 3:21pm UTC](https://www.ruby-forum.com/t/need-help-with-singleton/132314/4 "2008-03-19T15:21:56Z")

</div>

I forgot that, but well, it was working XD i was using puts to see the  
object id and it was working  
I changed now and still working 🙂

Thanks for the help 🙂

---

<div class="post-metadata">

**Author:** ![di3go](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a9a28c/32.png) [@di3go](https://www.ruby-forum.com/u/di3go)\
**Post date:** [March 19, 2008, 2:25pm UTC](https://www.ruby-forum.com/t/need-help-with-singleton/132314/5 "2008-03-19T14:25:21Z")

</div>

Np, my pleasure.

Something else. I’ve just scanned your code, maybe i’m wrong, but if  
@me is your singleton instance, shouldn’t it be a class variable? like  
maybe @@me?

Since its working, i’m most probably wrong. 🙂

Take care,  
Mt.

On Wed, Mar 19, 2008 at 8:06 PM, Diego B.

---

<div class="post-metadata">

**Author:** ![di3go](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a9a28c/32.png) [@di3go](https://www.ruby-forum.com/u/di3go)\
**Post date:** [March 28, 2008, 7:41am UTC](https://www.ruby-forum.com/t/need-help-with-singleton/132314/6 "2008-03-28T07:41:09Z")

</div>

On Wed, 19 Mar 2008 12:22:26 +0900, Diego B. wrote:

> Yea, again a question to help me building the game XD I have a class  
> called Mob, when i instance a mob from the main i search the name of mob  
> in a list, if the name is there i create it and i call a singleton class  
> with all the images of the mob, so if i create the same kind of mob  
> again he will create it and use the images from the singleton, to save  
> memory.

my understanding is that it’s good practice to write good code, use  
patterns, standard idioms and so forth, and _then_, if there’s a  
performance problem, start figuring out where the problem is.

I think you may have the horse before the cart in that sense.

Not that singleton isn’t a great thing, it is!

-Thufir
