Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The for loop is not the best way to approach iterating over an array in Ruby. With the clarification of your question, I think you have a few possibly strategies.</p> <p>You have two arrays, a and b. If both arrays are the same length:</p> <pre><code>a.each_index do |index| if a[index] == b[index] do something else do something else end end </code></pre> <p>This also works if A is shorter than B.</p> <p>If you don't know which one is shorter, you could write something like:</p> <p><code>controlArray = a.length &lt; b.length ? a : b</code> to assign the controlArray, the use controlArray.each_index. Or you could use <code>(0..[a.length, b.length].min).each{|index| ...}</code> to accomplish the same thing.</p> <hr> <p>Looking over your edit to your question, I think I can rephrase it like this: given an array with duplicates, how can I obtain a count of each item in each array and compare the counts? In your case, I think the easiest way to do that would be like this:</p> <pre><code>a = [:a,:a,:a,:b,:b,:c,:c,:d,:e,:e,:e] b = [:a,:a,:b,:b,:b,:c,:c,:c,:d,:e,:e,:e] not_alike = [] a.uniq.each{|value| not_alike &lt;&lt; value if a.count(value) != b.count(value)} not_alike </code></pre> <p>Running that code gives me <code>[:a,:b,:c]</code>. </p> <p>If it is possible that a does not contain every symbol, then you will need to have an array which just contains the symbols and use that instead of a.uniq, and another <code>and</code> statement in the conditional could deal with nil or 0 counts.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload