From: "Chr. Rippel" Date: 2002-01-26T18:32:51+09:00 Subject: Re: Serious Array Bug in Ruby 1.6.6? "William Djaja Tjokroaminata" wrote in message news:a2sli3$7kf$1@grapevine.wam.umd.edu... > Christoph, > > Thank you, thank you for summarizing it quite well. For me with C/C++ > background, it is much easier to just say all Ruby references or variables > are pointers (except the FixNum, True, etc.) So basically Ruby only > supports > > ... > pointer = &(new (object)); > vector a = new (n, pointer); > > and not > > vector a = new (n, object); > > (excuse me if the syntax is wrong; I have not used C++ for a long time). > > Yes, the List class is exactly what I want. I don't see why it is so > inefficient, because the opposite is I still don't see the use of an array > of pointers to the same object. If it is so inefficient, why just don't > provide it to the user: Array.new (n, obj) only works for obj of FixNum, > True, Nil, etc. On the other hand, if it is allowed for obj to be a Hash, > then why not create multiple hashes instead of just a single hash. All of these objects are very special - true,false and nil are the instance of classes that implement the Singleton pattern - for example, cloning and duping are a priori prohibited. FixNums are actually not special (in this respect) - they just apear this way. ---- class Fixnum attr_accessor :var alias __to_s to_s def to_s unless defined? @var __to_s else "#{__to_s} and @var = #{@var}" end end alias inspect to_s private :__to_s end L = Array.new (3,1) p L L[0].var= "a" p L ---- [1, 1, 1] [1 and @var = a, 1 and @var = a, 1 and @var = a] ---- > > Ruby has done a lot of checkings, like public/protected/private methods, > and attr_reader/attr_accessor methods. For Array, why not just check the > type/class of obj when the array is created, just like what you do > for singleton? If a program has to create and destroy arrays many many > times, probably it should have been written in C. On the other hand why not add this type of functionality yourself ---- class Array def self.my_new (nums) new(nums).collect! {yield} end end class A; end p Array.my_new(3){ A.new } ---- [#, #, #] ---- /Christoph