[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

singleton is this easy?

Thufir Hawat

11/9/2007 3:55:00 AM

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

5 Answers

pphetra

11/9/2007 9:45:00 AM

0

On Nov 9, 10:55 am, Thufir <hawat.thu...@gmail.com> wrote:
> Is this all that's required to ensure that there's ever only one
> instance of an ArrayOfCreatures?
yes.

Peter Szinek

11/9/2007 10:00:00 AM

0

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.rubyra...
http://s...


7stud --

11/9/2007 7:51:00 PM

0

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

--
Posted via http://www.ruby-....

list. rb

11/9/2007 9:33:00 PM

0

Note: parts of this message were removed by the gateway to make it a legal Usenet post.

Posting code without indentation? wtf?

On Nov 9, 2007 2:51 PM, 7stud -- <bbxx789_05ss@yahoo.com> wrote:

> 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
>
> --
> Posted via http://www.ruby-....
>
>

Justin Collins

11/10/2007 1:57:00 AM

0

On Nov 9, 2007 2:51 PM, 7stud -- <bbxx789_05ss@yahoo.com> 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