English 中文(简体)
替换阵列中的内容(原文)
原标题:Replace elements in array (ruby)
  • 时间:2011-11-23 15:48:18
  •  标签:
  • ruby
  • arrays

我有2个阵列,我想把它们合并起来,这样,由此产生的阵列将包含第一阵列中的所有内容,以取代第二阵列具有相同目标的任何内容。

  finalArr=[]
  arr1.each do |e1|
    set2Contains=false
    arr2.each do |e2|
      if(e2.id==e1.id)
        set2Contains=true
      end
    end
    if(set2Contains)
      finalArr.push(e2)
    else
      finalArr.push(e1)
    end
  end

我是新 rub,但是由于我是一位统治者的国王,上述情况似乎很少。 我很想知道,我的法典能否以任何方式缩短/优化?

感谢任何建议

最佳回答

你们想把第二阵列放在身份证上,因此,你每次都无法通过:

hash = Hash.new
arr2.collect{|x| hash[x.id] = x}

那么,你可以走到前面,做的是:

finalArr = arr1.map{|x| hash.has_key?(x.id) ? hash[x.id] : x }

请注意,如果贵国的阵列能够包含<代码>nil,可能会有警告,在这种情况下,我认为情况并非如此。

问题回答

2. 模仿逻辑,但经过改进的代码:

final_arr=[]
arr1.each do |e1|
  if arr2.any? { |e2| e1.id == e2.id }
    final_arr << e2
  else
    final_arr << e1
  end
end

更简洁

final_arr=[]
arr1.each do |e1|
  final_arr << arr2.any? { |e2| e1.id == e2.id } ? e2 : e1
end

由于你提到一行,这里的功能是:

merged = Hash[ a1.map{|o| [o.id,o]} ].merge(Hash[ a2.map{|o| [o.id,o]} ]).values

这把两个阵列都转换成由<代码>id钥匙的斜体,将其合并(从<代码>a2中超过<代码>a1的数值,然后仅提取数值。

如果你再与这些物体做许多类似的工作,我建议你界定<条码>以下l?和<条码>hash,以比较它们的<条码>id数值,然后使用内联网 rel=“nofollow”<条码>><<<>><<>>>><>>>>><>>>>>>>>>>>>>>>>>>>>>>>>>>>>><>>>><>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 班级:

require  set 

Foo = Struct.new(:id,:name) do
  def eql?(o2)
    id==o2.id
  end
  def hash
    id.hash
  end
end

a1 = Set[ Foo.new(1,"Phrogz"), Foo.new(17,"Cat")   ]
a2 = Set[ Foo.new(42,"Arthur"), Foo.new(1,"Gavin") ]

all = a1 + a2
all.each{ |foo| puts foo }
#=> #<struct Foo id=1, name="Phrogz">
#=> #<struct Foo id=17, name="Cat">
#=> #<struct Foo id=42, name="Arthur">

1.9 简明扼要:

(a|b).uniq{|x| x[:id]}

将阵列与你不想首先取代的价值。





相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

热门标签