Project

General

Profile

Actions

Feature #21157

open

Comparison operator <>

Added by lpogic (Łukasz Pomietło) about 12 hours ago. Updated about 9 hours ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:121172]

Description

I propose introducing a comparison operator <> which would give the following results:

1 <> 2 # => -1
2 <> 1 # => 1
1 <> 1 # => false
1 <> "a" # => true

With the help of the new operator, complex ordering expressions could be written explicitly. For example:

Point = Struct.new(:x, :y)
array = [Point.new(1, 2), Point.new(6, 4), Point.new(2, 2), Point.new(5, 2)]

array.sort{|a, b| a.y <> b.y || b.x <> a.x || 0 } # => [#<struct Point x=5, y=2>, #<struct Point x=2, y=2>, #<struct Point x=1, y=2>, #<struct Point x=6, y=4>]

The <> notation may look familiar to sql users, where it means 'not equal'. Defined in the form given it will retain this meaning to some extent:

a = b = 1
a_not_equal_b if a <> b
a_equal_b unless a <> b
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like1