------art_107932_477784.1153430342771
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

On 7/20/06, Ben Zealley <transhumanist / gmail.com> wrote:
>
> Is there a nice elegant way of creating several named objects of the
> same class? I naively tried
>
> a,b,c,d = 0
> [a, b, c, d].each { |o| o = SomeClass.new }
>
> and found that while they get initialised inside the block, they get
> destroyed leaving it. I can't believe
>
> a = SomeClass.new
> b = SomeClass.new
> etc.
>
> is the best way to do it. I can populate an array, but let's
> hypothesise that for reasons relating to irritating corporate coding
> standards, the variables need specific names... ;)
>
> Thoughts appreciated! Cheers
>
> --
> Ben
>
>
>
I prefer to write

class Class
    def *(num)
        a = []
        num.times do
            a << new
        end
        a
     end
end

a, b, c = String * 3
###########
a << "hi"
puts a.inspect
puts b.inspect
puts c.inspect

too bad I cannot work out how to write

a, b, c = String * :many


Cheers
Robert
-- 
Deux choses sont infinies : l'univers et la bóŐise humaine ; en ce qui
concerne l'univers, je n'en ai pas acquis la certitude absolue.

- Albert Einstein

------art_107932_477784.1153430342771--