Compare Ruby Arrays Online

Paste two Ruby arrays (as JSON). See what was added, removed, or changed — element by element.

🔒 100% private — runs entirely in your browser

or try sample data

What is Ruby Array Compare?

Ruby Array Compare diffs two Ruby arrays element by element and shows you exactly what changed. Ruby provides the - operator for array difference, but it only shows elements in the first array not present in the second — it doesn't show additions, modifications, or preserve index positions.

Ruby arrays are ordered, integer-indexed collections that can hold any object — strings, numbers, hashes, other arrays, or even mixed types. Ruby's expressive array methods (map, select, reject, zip) are powerful, but none give you a visual diff of two arrays.

Paste your arrays as JSON (e.g., ["a", "b", "c"]). Convert Ruby symbols to strings and hashes to JSON objects. The tool compares at any nesting depth.

Ruby Array Comparison — Code Examples

Array subtraction with -

arr1 = ["rails", "sinatra", "rspec", "sidekiq"]
arr2 = ["rails", "hanami", "rspec", "sidekiq", "dry-rb"] removed = arr1 - arr2 # ["sinatra"]
added = arr2 - arr1 # ["hanami", "dry-rb"] puts "Removed: #{removed}"
puts "Added: #{added}"

Array#- removes matching elements but doesn't show position or handle nested structures.

Set operations with & and |

require 'set' a = Set.new([1, 2, 3, 4])
b = Set.new([3, 4, 5, 6]) puts (a - b).to_a # [1, 2] — only in a
puts (b - a).to_a # [5, 6] — only in b
puts (a & b).to_a # [3, 4] — in both
puts (a | b).to_a # [1, 2, 3, 4, 5, 6] — union

Set operations are clean but discard duplicates and ordering information.

Index-aware comparison with each_with_index

arr1 = ["rails", "sinatra", "rspec"]
arr2 = ["rails", "hanami", "rspec", "puma"] max = [arr1.length, arr2.length].max
max.times do |i| v1 = i < arr1.length ? arr1[i] : "(missing)" v2 = i < arr2.length ? arr2[i] : "(missing)" puts "[#{i}]: #{v1} -> #{v2}" if v1 != v2
end
# [1]: sinatra -> hanami
# [3]: (missing) -> puma

Manual index comparison works for flat arrays but breaks down with nested hashes.

Ruby Array Comparison Gotchas

Symbol vs String confusion

:foo == "foo" is false in Ruby — symbols and strings are different types. When pasting data here, convert symbols to strings. JSON does not have a symbol type.

Array#- removes all occurrences

[1, 1, 2, 3] - [1] returns [2, 3], removing both 1s. If you need to remove only the first occurrence, use delete_at with index, or use this tool for a position-aware diff.

Hash ordering changed in Ruby 1.9+

Since Ruby 1.9, hashes maintain insertion order. When comparing arrays of hashes, the order of keys affects == comparison. {a: 1, b: 2} == {b: 2, a: 1} is true in Ruby, but JSON stringification order may differ.

Frequently Asked Questions

How do I compare two Ruby arrays online?

Paste your Ruby arrays as JSON (e.g., ["a", "b", "c"]) into the two panels and click Compare. Convert Ruby symbols to strings and hashes to JSON objects.

What does the - operator do for Ruby arrays?

Array#- returns elements in the first array that are not in the second array. It removes all occurrences and doesn't show additions or index-level changes.

Can this compare arrays of Ruby hashes?

Yes. Convert hashes to JSON objects ({"key" => "value"} becomes {"key": "value"}). The tool compares all key-value pairs recursively.

Is my data safe?

Yes. This tool runs entirely in your browser. No data is sent to any server.

Does this handle nested arrays?

Yes. The tool performs deep recursive comparison, showing changes at any nesting depth.

Can I ignore array order?

Yes. Check "Ignore array order" to compare arrays as unordered collections, similar to Ruby Set comparison.