# Singleton is this easy?

**URL:** <https://www.ruby-forum.com/t/singleton-is-this-easy/119680>\
**Category:** Ruby\
**Created:** [November 9, 2007, 5:01am UTC](https://www.ruby-forum.com/t/singleton-is-this-easy/119680 "2007-11-09T05:01:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [November 9, 2007, 5:01am UTC](https://www.ruby-forum.com/t/singleton-is-this-easy/119680/1 "2007-11-09T05:01:22Z")

</div>

Is this all that’s required to ensure that there’s ever only one  
instance of an ArrayOfCreatures? Seems too easy!

C:\code\>  
C:\code\>  
C:\code\>type Driver.rb  
require ‘ArrayOfCreatures’

require ‘Dragon’

someCreatures=ArrayOfCreatures.instance

puts “\nquantity of creatures:”  
numOfCreatures = gets.chomp.to\_i

0.upto(numOfCreatures) do |i|  
someCreatures[i]=Dragon.new  
end

someCreatures.toString  
C:\code\>  
C:\code\>  
C:\code\>type ArrayOfCreatures.rb  
require ‘singleton’

class ArrayOfCreatures \< Array

include Singleton

def toString

```
    print "\n\n\nArrayOfCreatures\n"
    print "================\n\n"

    i=0
    while i < self.length
            print "\n\n"
            print i
            print ":\n"
            print self[i].toString
            i=i+1
    end

```

end

end  
C:\code\>  
C:\code\>

thanks,

Thufir

---

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [November 9, 2007, 10:51am UTC](https://www.ruby-forum.com/t/singleton-is-this-easy/119680/2 "2007-11-09T10:51:01Z")

</div>

On Nov 9, 10:55 am, Thufir [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Is this all that’s required to ensure that there’s ever only one  
> instance of an ArrayOfCreatures?  
> yes.

---

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [November 9, 2007, 11:00am UTC](https://www.ruby-forum.com/t/singleton-is-this-easy/119680/3 "2007-11-09T11:00:58Z")

</div>

Thufir wrote:

> Is this all that’s required to ensure that there’s ever only one  
> instance of an ArrayOfCreatures? Seems too easy!

You can also use the standard Ruby singleton implementation:

=============================================================  
require ‘singleton’

class SavageCrystalDragon  
include Singleton  
end

# scd = SavageCrystalDragon.instance

If you would try to scd.dup:

can’t dup instance of singleton SavageCrystalDragon (TypeError)

HTH,  
Peter

* * *

[http://www.rubyrailways.com](http://www.rubyrailways.com)  
[http://scrubyt.org](http://scrubyt.org)

---

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [November 9, 2007, 8:51pm UTC](https://www.ruby-forum.com/t/singleton-is-this-easy/119680/4 "2007-11-09T20:51:11Z")

</div>

Thufir wrote:

> Is this all that’s required to ensure that there’s ever only one  
> instance of an ArrayOfCreatures? Seems too easy!

Implementing a singleton class by hand is easy too, so you aren’t  
getting much savings using the Singleton module–you save yourself from  
having to write 4 extra lines of code.

If I have a method with 200 lines of code in it, and I put the method in  
its own module Calculations, I can perform the complicated calculation  
by writing:

require ‘calculations’  
result = my\_meth(10, 20, 3.5)

That’s pretty easy too, isn’t it?

On a side note: when you ask questions, you want to make it as easy as  
possible for people to answer your questions, right? Posting code  
without indentation is bad form because it makes your code harder to  
read. Some people won’t even bother with your questions if you can’t be  
bothered with posting indented code. In Ruby, the recommended style is  
to use 2 spaces for indenting. Your code would look look something like  
this:

require ‘singleton’

class ArrayOfCreatures \< Array  
include Singleton

def toString  
print “\n\n\nArrayOfCreatures\n”  
print “================\n\n”

```
i=0
while i < self.length
  print "\n\n"
  print i
  print ":\n"
  print self[i].toString
  i=i+1
end

```

end  
end

---

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [November 9, 2007, 10:33pm UTC](https://www.ruby-forum.com/t/singleton-is-this-easy/119680/5 "2007-11-09T22:33:57Z")

</div>

Posting code without indentation? wtf?

---

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [November 10, 2007, 2:57am UTC](https://www.ruby-forum.com/t/singleton-is-this-easy/119680/6 "2007-11-10T02:57:05Z")

</div>

On Nov 9, 2007 2:51 PM, 7stud – [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > On a side note: when you ask questions, you want to make it as easy as  
> > possible for people to answer your questions, right? Posting code  
> > without indentation is bad form because it makes your code harder to  
> > read. Some people won’t even bother with your questions if you can’t be  
> > bothered with posting indented code. In Ruby, the recommended style is  
> > to use 2 spaces for indenting. Your code would look look something like  
> > this:  
> > …

list. rb wrote:

> Posting code without indentation? wtf?

Yeah, I see the indentation on both the mailing list and the forums in  
the OP and the replies, so I don’t understand where this is coming from.

-Justin
