Wednesday, October 21, 2009

Simple Array Comparison in Ruby

Here is a simple method to compare two arrays using Ruby, few examples:

> [1,2,3] <=> [1,2,3]
=> 0
> [1,2,3] <=> [2,2,3]
=> -1
> [1,2,3] <=> [3,2,3]
=> -1
> [1,2,3] <=> [1,3,3]
=> -1
> [1,2,3] <=> [1,1,3]
=> 1

By use of Terinary Operator (<=>), we can acheive the simple method to compare two arrays of comparison.

Hope this will help for someone or me in future!!!

No comments: