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...
I am sure some of the real Ruby hackers will have a better solution,
but you can do something like:
a,b,c = Array.new(3).map { Foo.new(42) }
pth
···
On 7/20/06, Ben Zealley <transhumanist@gmail.com> wrote:
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...
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
The parallel assignment/array expansion approach suggested by patrick and james will definitely work. You could also try
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...
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
###########
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êtise humaine ; en ce qui
concerne l'univers, je n'en ai pas acquis la certitude absolue.
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
The parallel assignment/array expansion approach suggested by patrick and james will definitely work. You could also try
losing both the .map and the |i|, and that still works (go Ruby! I
continue to be amazed by how much it can work out...). But all of these
leave an anonymous array of the variables sitting around, which doesn't
get GC'd. Can it be done without that, or should I reduce my pedantic
tendencies slightly and live with it?
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
The parallel assignment/array expansion approach suggested by patrick and james will definitely work. You could also try
losing both the .map and the |i|, and that still works (go Ruby! I
continue to be amazed by how much it can work out...). But all of these
leave an anonymous array of the variables sitting around, which doesn't
get GC'd. Can it be done without that, or should I reduce my pedantic
tendencies slightly and live with it?
The array holds references to the new objects, but the objects don't know
that there is an array:
···
-------------------------------------------
GC.start
p ObjectSpace.each_object(Array) {}
a, b, c = Array.new(3) { Object.new }
p ObjectSpace.each_object(Array) {}
GC.start
p ObjectSpace.each_object(Array) {}
-------------------------------------------
On 7/20/06, transfire@gmail.com <transfire@gmail.com> wrote:
> class Proc
> def *(i)
> a =
> i.times{ a << call }
> a
> end
> end
>
> lambda{ Object.new } * 3
>
> Which is pretty versitle. Unforunately I already have Proc#* tied up
> with function composition.
There's no conflict:
class Proc
def *(x)
if Integer===x
(0...x).map{call}
else
#...compose procs
proc{|*args| self[x[*args]]}
end
end
end
Hmm...It's too bad that #times below returns 3. It be pretty neat if it
acted like #map in this respect instead.
3.times { Object.new }
irb(main):001:0> RUBY_VERSION
=> "1.9.0"
irb(main):002:0> a, b, c = 3.times.map { Object.new }
=> [#<Object:0x2382c8>, #<Object:0x2381d8>, #<Object:0x238070>]
Interesting.... That reveals that one of the problems with this magic
enumerator thing is that the method names weren't necessarily chosen
with this in mind, and don't work very well. 3.times.map very
strongly does *not* communicate a 0...3 mapping to me. My first
reading is:
3.map
since I expect 3.times to return 3. My second reading is:
3.times { map { Object.new } }
which also makes no sense. It's quite a leap to figure out that it
means:
(0...3).map { Object.new }
I fear the magic enumerator doesn't sit very well with the language as
designed without it.
David
···
On Fri, 21 Jul 2006, Logan Capaldo wrote:
On Jul 20, 2006, at 7:32 PM, transfire@gmail.com wrote: