0

I am currently trying to compare every element of an array with the others (in Ruby). Those elements are objects of a class. I need to find similarities between them. My idea was to loop through the original array and in this loop creating a new array containing the other elements (not the one of the outer loop) and then loop through this second array and compare every item with the one in the outer each loop.

Here is some pseudocode:

originalArray.each{
    |origElement|
    tempArray = createNewArray from original array without origElement
    tempArray.each{
        |differentElement|
        Compare origElement with differentElement
    }
}

How can I create that tempArray?

1
  • When you ask a question about code you need, don't supply pseudo-code, show what you've actually tried. Also, in Ruby, we don't use camelCase for variables, we use snake_case. It's idiomatic and using camelCase would get you dinged in a code-review or when applying for a job. Code style depends on where you work and the defined styles for a particular language. We don't make 'em up as we go just because we're more comfortable with something. Commented Dec 1, 2013 at 21:37

2 Answers 2

2

I think you should use Array#permutation for this

original_array.permutation(2) { |elements| Compare elements[0] with elements[1] }
Sign up to request clarification or add additional context in comments.

4 Comments

But just to get this straight: Would this actually be all I need? I mean is this the same as an each-loop? If I have an array containing of o1,o2,o3 and o4 could I compare all of them, by comparing elements[0] and elements[1]?
@stiller_leser permutation as explained in the ruby docs, When invoked with a block, yield all permutations of length n of the elements of the array, then return the array itself. So what happens is since you want to compare pairs, it would yield a pair of each elements in your array continually for you to compare till they are exhausted. Permutation is an enumerable so you don't need the each-loop in the real sense as it is implemented in the background, so this is all you need
If a and b are elements of the array, permuatation(2) will iterate over [a,b] and [b,a]. If it is only necessary to iterate over each pair once, use combination(2) instead.
I actually ended up using @CarySwoveland answer - wasn't clear in my question though. It did save me some tim though.
1

First, I want to say bjhaid's answer is beautiful and for your specific instance, it is the one that should be used.

However, I wanted to provide a more general answer that answers the direct question you asked: "How can I create that tempArray?"

If you wanted to delete all values that are equal to the element in the original array, you could simply do:

tempArray = originalArray - [origElement]

However, if you only want to delete that element, you could do:

originalArray.each_with_index {
  |origElement, index|
  tempArray = originalArray.dup
  tempArray.delete_at(index)
  tempArray.each{
    |differentElement|
      Compare origElement with differentElement
  }
} 

Also, a note on styling. You probably want to use underscores instead of CamelCase for all methods/variables. In the Ruby community, CamelCase is typically reserved for class / module names. You also probably want to keep the "piped-in" variables (called block arguments) on the same line as the beginning of the block. It is certainly not a requirement, but it is an almost universal convention in the Ruby community.

This code snippet would be much more familiar and readable to your typical Ruby dev:

original_array.each_with_index do |orig_element, index|
  temp_array = original_array.dup
  temp_array.delete_at(index)
  temp_array.each do |different_element|
      Compare orig_element with different_element
  end
end

3 Comments

Hi, I think I will use the accepted answer, however I appreciate the additional info. CamelCase is just what I am used to and yes I am new to Ruby ;)
@stiller_leser careful. This does not seems to be right answer. Both delete and delete_at are returning the deleted element not the resulting array or copy of it. Moreover, they are destructive -- it would iterate not each object in original_array but every second (since on every iteration it deletes element and index increments independetly).
@user2422869 Thank you! The lack of an exclamation mark led me to assume that delete and delete_at were non-destructive. That is what I get for assuming. I have updated the answer accordingly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.