如果项目存在,则运行代码x次

时间:2016-09-01 13:07:08

标签: ruby-on-rails ruby postgresql ruby-on-rails-5

从我的last question,我似乎无法得到我想要的东西。下面解释得更好:

external = ["apple"]
internal = ["grapes", "pear", "mangoes", "apple"]

external.each do |fruit|
 if not internal.include?("apple") # fruit will be in place of apple.
   puts "yes"
   # run code
 end
end

到目前为止这是打印一次。怎么打印三次?在英语中,我说如果apple存在,请单独留下apple,给我其他人,然后运行下面的代码。如果有4个项目,其中一个是apple,则运行代码三次。如果apple不存在,请运行代码4次。

我希望这很清楚。感谢

我需要100%准确

内部是模特(postgres) 外部是shopify api(数组)

我已将shopify的产品ID保存在我的数据库Bar中,名为fooBar.first.foo给了我shopify的ID。 Bar将有/不具有shopify ID的其他对象。所以如果shopify在那里,请给我休息。这就是我提出的原因:

external.each do |fruit|
 if not internal.include?("apple") # fruit will be in place of apple.
   puts "yes"
   # run code
 end
end

我的代码的精确编辑:

externalshopify product response

external = # shopify's response (forget about the above example).
internal = Product.all # All of the user's products

所以在我的产品型号中,我有一个产品ID。该ID适用于shopify产品。例如:

Product.first.product_id = # "8cd66767sssxxxxx"

在同一个型号(产品)中,我有更多的对象,以及shopify产品ID。我需要除了8cd66767sssxxxxx之外的所有对象:

Product.last.product_id = # "BOOK" etc but not a shopify id.

fl00r的答案适用于我的控制台,但不适用于控制器。奇怪的。这对我有用。

external.each do |e|
  internal.each do |i|
    puts i.product_id unless i.product_id == e.id.to_s
  end
end

小问题。在第一次迭代中,包含id但在第二次迭代时不包括public class MainActivity extends AppCompatActivity { private TextView englishString, hindiString; private Button englishButton, hindiButton; private TextToSpeech textToSpeech; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); englishString = (TextView) findViewById(R.id.englishString); hindiString = (TextView) findViewById(R.id.hindiString); englishButton = (Button) findViewById(R.id.englishButton); hindiButton = (Button) findViewById(R.id.hindiButton); englishButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { loadSpeakingLanguages(englishString.getText().toString()); } }); hindiButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { loadSpeakingLanguages(hindiString.getText().toString()); } }); } private void loadSpeakingLanguages(String textToTranslate) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { ttsGreater21(textToTranslate); } else { ttsUnder20(textToTranslate); } } @SuppressWarnings("deprecation") private void ttsUnder20(String text) { HashMap<String, String> map = new HashMap<>(); map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "MessageId"); textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, map); } @TargetApi(Build.VERSION_CODES.LOLLIPOP) private void ttsGreater21(String text) { String utteranceId = this.hashCode() + ""; textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null, utteranceId); } @Override protected void onResume() { textToSpeech = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() { @Override public void onInit(int status) { if (status != TextToSpeech.ERROR) { textToSpeech.setLanguage(Locale.ENGLISH); } } }); super.onResume(); } public void onPause() { if (textToSpeech != null) { textToSpeech.stop(); textToSpeech.shutdown(); textToSpeech = null; } super.onPause(); }} 。不知道该怎么做。

2 个答案:

答案 0 :(得分:3)

为什么不这样做?

(internal - external).each do |fruit| 
  puts 'yes' 
  #your code
end

答案 1 :(得分:1)

另一种方法是重新考虑您的外部数据结构。 Set在这里会更合适(我们会使用Hash):

external = ["apple"]
internal = ["grapes", "pear", "mangoes", "apple"]

external_hash = external.each_with_object({}){ |o, h| h[o] = true }

internal.each do |item|
  unless external_hash[item]
    p item
  end
end
#=> "grapes"
#=> "pear"
#=> "mangoes"

或者您可以先过滤内部列表

internal_filtered = internal.reject{ |item| external_hash[item] }
internal_filtered.each do |item|
  p item
end
#=> "grapes"
#=> "pear"
#=> "mangoes"

Array转换为Hash将花费O(n),并且每次后续查询都需要摊销O(1)

总复杂度为O(n+m),其中n是外部列表的大小,m是内部列表的大小。