diff a ruby string or array
diff.rb is what you want, which is available at http://users.cybercity.dk/~dsl8950/ruby/diff.html via internet archive:
http://web.archive.org/web/20140421214841/http://users.cybercity.dk:80/~dsl8950/ruby/diff.html
Ruby difference in two string arrays and pass to the method
(current_owners - OWNERS).each { |new_owner| new_owner_alarm(new_owner) }
Get difference of arrays in Ruby
irb(main):001:0> a = [1, 2, 3, 4, 5]
=> [1, 2, 3, 4, 5]
irb(main):002:0> b = [1, 2, 4, 6]
=> [1, 2, 4, 6]
irb(main):003:0> a - b
=> [3, 5]
irb(main):005:0> b - a
=> [6]
irb(main):006:0>
Finding the difference between strings in Ruby
All of the solutions so far ignore the fact that the second array can also have elements that the first array doesn't have. Chuck has pointed out a fix (see comments on other posts), but there is a more elegant solution if you work with sets:
require 'set'
teamOne = "Billy, Frankie, Stevie, John"
teamTwo = "Billy, Frankie, Stevie, Zach"
teamOneSet = teamOne.split(', ').to_set
teamTwoSet = teamTwo.split(', ').to_set
teamOneSet ^ teamTwoSet # => #<Set: {"John", "Zach"}>
This set can then be converted back to an array if need be.
Ruby difference in array including duplicates
I am so glad you asked. I would like to see such a method added to the class Array
in some future version of Ruby, as I have found many uses for it:
class Array
def difference(other)
h = other.each_with_object(Hash.new(0)) { |e,h| h[e] += 1 }
reject { |e| h[e] > 0 && h[e] -= 1 }
end
end
A description of the method and links to some of its applications are given here.
By way of example:
a = [1,2,3,4,3,2,4,2]
b = [2,3,4,4,4]
a - b #=> [1]
a.difference b #=> [1,2,3,2]
Ruby v2.7 gave us the method Enumerable#tally, allowing us to replace the first line of the method with
h = other.tally
Ruby: How to find difference between 2 arrays, order matters
how about this
array_one = ["A", "B", "C"]
array_two = ["A", "C", "A"]
array_one.select.each_with_index { |item, index|
array_two[index] != item
} => #B and C
Difference between += for Integers/Strings and For Arrays?
You see, names (variable names, like a
and b
) don't hold any values themselves. They simply point to a value. When you make an assignment
a = 5
then a
now points to value 5, regardless of what it pointed to previously. This is important.
a = 'abcd'
b = a
Here both a
and b
point to the same string. But, when you do this
a += 'e'
It's actually translated to
a = a + 'e'
# a = 'abcd' + 'e'
So, name a
is now bound to a new value, while b
keeps pointing to "abcd".
a = [1,2,3,4]
b = a
a << 5
There's no assignment here, method <<
modifies existing array without replacing it. Because there's no replacement, both a
and b
still point to the same array and one can see the changes made to another.
Find the only difference between two arrays in Ruby
I think this is the best built-in function to get what you expect
(a - b).pop
Related Topics
Aws S3: the Bucket You Are Attempting to Access Must Be Addressed Using the Specified Endpoint
How to Search Within an Array of Hashes by Hash Values in Ruby
Rails Activerecord: Find All Users Except Current User
Mountain Lion Rvm Install 1.8.7 X11 Error
Issue Updating Ruby on MAC With Xcode 4.3.1
How to Solve "Ruby Installation Is Missing Psych" Error
Heroku - Cannot Run Git Push Heroku Master
Ruby 1.9: How to Properly Upcase & Downcase Multibyte Strings
Why Is the Shovel Operator (≪≪) Preferred Over Plus-Equals (+=) When Building a String in Ruby
Measure the Distance Between Two Strings With Ruby
How to Seed a Database in Rails
What Is the Easiest Way to Remove the First Character from a String