1

有人可以解释一下,为什么我LIST从一开始的原始常数到最后都被操纵了?我认为常量可以只初始化一次。我想将操作存储在一个新数组 ( new_list) 中而不影响原始数组 ( LIST)。

$ned = "foo"
$med = ""

print LIST = [:nrd, :mrd_y] # -> [:nrd, :mrd_y]


list = LIST

new_list = list.delete_if { |element|
  case element
  when :nrd then $ned.empty?
  when :mrd_y then $ned.empty? || $med.empty?
  end
}

print new_list # -> [:nrd]

print LIST # -> [:nrd] instead of [:nrd, :mrd_y]
4

1 回答 1

2

Array#delete_if-> 删除块计算为 true 的每个 self 元素。

$ned = "foo"
$med = ""

LIST = [:nrd, :mrd_y]
p LIST.object_id #=> 84053120
list = LIST
p LIST.object_id #=> 84053120
new_list = list.delete_if { |element|
  case element
  when :nrd then $ned.empty?
  when :mrd_y then $ned.empty? || $med.empty?
  end
}

Listlist持有相同的Array对象,object_id如上所述。因此,对于来自块的每个true评估,都会从delete_if引用的对象中删除项目84053120。由LISTand保存list。所以你可以使用以下内容:

$ned = "foo"
$med = ""

LIST = [:nrd, :mrd_y] 
list = LIST
new_list = list.dup.delete_if { |element|
  case element
  when :nrd then $ned.empty?
  when :mrd_y then $ned.empty? || $med.empty?
  end
}

p new_list #=>[:nrd]
p LIST #=>[:nrd, :mrd_y]

或者(更好的方法使用Array#reject),

$ned = "foo"
$med = ""

list = [:nrd, :mrd_y]  
new_list = list.reject { |element|
  case element
  when :nrd then $ned.empty?
  when :mrd_y then $ned.empty? || $med.empty?
  end
}
p new_list #=>[:nrd]
p list #=>[:nrd, :mrd_y]
于 2013-05-18T17:11:52.370 回答