分配不同的值后,Ruby列表值是相同的

Nat*_*eir 0 ruby ruby-on-rails redis

抱歉这个令人困惑的标题,不知道如何描述这个问题.

在Ruby on Rails控制器中,我正在创建一个名为的列表@commits,其中每个项@commits应包含一个哈希表,其元素是每个提交的各种属性的值.这些属性值存储在Redis数据库中.

下面,我遍历一个属性列表,其中的值应该从Redis中获取,然后为8个不同的提交中的每一个获取这些值.然后我使用提交属性名称作为哈希的键,将redis中的值放入每个提交的不同哈希表中.

# Initialize @commits as a list of eight empty hash tables
@commits = Array.new(8, {})

# Iterate over the attributes that need hashed for each item in @commits
[:username, :comment, :rev, :repo].each do |attrib|
    # 8 items in @commits
    8.times do |i| 
       # Get a value from redis and store it in @commits[i]'s hash table
       @commits[i][attrib] = $redis.lindex(attrib, i)

       # Print the value stored in the hash
       # Outputs 7, 6, .., 0 for @commits[i][:rev]
       puts @commits[i][attrib].to_s
    end
end

# Print the value of every item that was stored in the hash tables above, 
# but only for the :rev key
# Outputs 0 eight times
8.times do |i|
    puts @commits[i][:rev]
end
Run Code Online (Sandbox Code Playgroud)

但是,根据上面的评论,@ commit [0..7]似乎在它们的哈希值中都有相同的值,尽管它们似乎在上面的几行中正确存储.使用散列键:rev作为示例,第一个puts输出7..0,这是正确的,但第二个puts输出数字0八次.

谁知道为什么?

tad*_*man 5

如果你展示如何@commits初始化它会有所帮助,但看起来你已经创建了一个对同一个对象有多个引用的结构.

为所有键回收的错误,相同的对象:

@commits = Hash.new([ ])
Run Code Online (Sandbox Code Playgroud)

为每个键创建正确的新对象:

@commits = Hash.new { |h, k| h[k] = [ ] }
Run Code Online (Sandbox Code Playgroud)

您可能使用具有相同错误的Array:

@commits = Array.new(8, [ ])
Run Code Online (Sandbox Code Playgroud)

这将导致以下行为:

a = Array.new(4, [ ])
a[0]
# => []
a[0] << 'x'
# => ["x"]
a
# => [["x"], ["x"], ["x"], ["x"]]
Run Code Online (Sandbox Code Playgroud)

它可以通过传入一个块来修复:

a = Array.new(4) { [ ] }
a[0]
# => []
a[0] << 'x'
# => ["x"]
a
# => [["x"], [], [], []]
Run Code Online (Sandbox Code Playgroud)

但是,看到使用值预先初始化的数组是非常罕见的.通常这些只是延迟初始化,或者使用Hash代替数组.