In Ruby, why does Array.new(size, object) create an array consisting of multiple references to the same object?

前端 未结 3 1842
陌清茗
陌清茗 2020-11-30 14:19

As mentioned in this answer, Array.new(size, object) creates an array with size references to the same object.

hash =          


        
相关标签:
3条回答
  • 2020-11-30 14:43

    For certain classes that can't be modified in-place (like Fixnums) the Array.new(X, object) form works as expected and is probably more efficient (it just calls memfill instead of rb_ary_store and yielding to the block):

    For more complicated objects you always have the block form (e.g. Array.new(5) { Hash.new }).

    *Edit:* Modified according to the comments. Sorry for the stupid example, I was tired when I wrote that.

    0 讨论(0)
  • 2020-11-30 14:55

    Because that's what the documentation says it does. Note that Hash.new is only being called once, so of course there's only one Hash

    If you want to create a new object for every element in the array, pass a block to the Array.new method, and that block will be called for each new element:

    >> a = Array.new(2) { Hash.new }
    => [{}, {}]
    >> a[0]['cat'] = 'feline'
    => "feline"
    >> a
    => [{"cat"=>"feline"}, {}]
    >> a[1]['cat'] = 'Felix'
    => "Felix"
    >> a
    => [{"cat"=>"feline"}, {"cat"=>"Felix"}]
    
    0 讨论(0)
  • 2020-11-30 14:59

    I came up with this answer, very short and simple solution

    c_hash = Hash.new
    ["a","b","c","d","e","f"].each do |o|
      tmp = Hash.new
      [1,2,3].map {|r| tmp[r] = Array.new}
      c_hash[o] = tmp
    end
    c_hash['a'][1] << 10
    p c_hash
    
    0 讨论(0)
提交回复
热议问题