Ruby比较运算符 #
一、比较运算符概述 #
Ruby提供了丰富的比较运算符,用于比较两个值的关系。
| 运算符 | 描述 | 示例 |
|---|---|---|
| == | 相等 | 1 == 1 |
| != | 不等 | 1 != 2 |
| < | 小于 | 1 < 2 |
| > | 大于 | 2 > 1 |
| <= | 小于等于 | 1 <= 1 |
| >= | 大于等于 | 2 >= 1 |
| <=> | 太空船 | 1 <=> 2 |
| === | case相等 | (1…5) === 3 |
| eql? | 相等(类型) | 1.eql?(1.0) |
| equal? | 同一对象 | a.equal?(b) |
二、相等比较 #
2.1 == 运算符 #
ruby
1 == 1
1 == 2
"hello" == "hello"
"hello" == "HELLO".downcase
[1, 2] == [1, 2]
{a: 1} == {a: 1}
nil == nil
true == true
2.2 != 运算符 #
ruby
1 != 2
1 != 1
"hello" != "world"
nil != false
2.3 eql? 方法 #
ruby
1.eql?(1)
1.eql?(1.0)
"hello".eql?("hello")
[1, 2].eql?([1, 2])
2.4 equal? 方法 #
ruby
a = "hello"
b = "hello"
c = a
a.equal?(b)
a.equal?(c)
a == b
2.5 == vs eql? vs equal? #
ruby
a = 1
b = 1.0
a == b
a.eql?(b)
a.equal?(a)
s1 = "hello"
s2 = "hello"
s1 == s2
s1.eql?(s2)
s1.equal?(s2)
三、大小比较 #
3.1 数字比较 #
ruby
1 < 2
2 > 1
1 <= 1
2 >= 1
1 < 2 < 3
3.2 字符串比较 #
ruby
"a" < "b"
"apple" < "banana"
"A" < "a"
"hello" < "hello world"
3.3 时间比较 #
ruby
require 'time'
t1 = Time.now
t2 = t1 + 3600
t1 < t2
t1 > t2
t1 == t1
3.4 between? #
ruby
5.between?(1, 10)
5.between?(5, 10)
5.between?(6, 10)
"a".between?("a", "z")
四、太空船运算符 #
4.1 基本用法 #
ruby
1 <=> 2
2 <=> 1
1 <=> 1
"a" <=> "b"
"b" <=> "a"
"a" <=> "a"
4.2 排序应用 #
ruby
arr = [3, 1, 4, 1, 5, 9, 2, 6]
arr.sort
arr.sort { |a, b| a <=> b }
arr.sort { |a, b| b <=> a }
4.3 自定义比较 #
ruby
class Person
attr_reader :name, :age
def initialize(name, age)
@name, @age = name, age
end
def <=>(other)
age <=> other.age
end
end
people = [
Person.new("Alice", 30),
Person.new("Bob", 25),
Person.new("Charlie", 35)
]
people.sort.map(&:name)
people.sort { |a, b| b <=> a }.map(&:name)
五、case相等运算符 #
5.1 基本用法 #
ruby
(1..5) === 3
(1..5) === 6
String === "hello"
Integer === 42
/ Ruby/ === "Hello Ruby"
5.2 case语句 #
ruby
def check(value)
case value
when 1..10
"1-10"
when String
"字符串"
when / Ruby/
"包含Ruby"
when :symbol
"符号"
else
"其他"
end
end
check(5)
check("hello")
check("Hello Ruby")
check(:symbol)
5.3 类匹配 #
ruby
String === "hello"
Integer === 42
Array === [1, 2, 3]
Hash === { a: 1 }
NilClass === nil
TrueClass === true
六、自定义比较 #
6.1 实现Comparable #
ruby
class Money
include Comparable
attr_reader :amount, :currency
def initialize(amount, currency = "USD")
@amount = amount
@currency = currency
end
def <=>(other)
return nil unless currency == other.currency
amount <=> other.amount
end
def to_s
"#{amount} #{currency}"
end
end
m1 = Money.new(100)
m2 = Money.new(200)
m3 = Money.new(150)
m1 < m2
m2 > m3
m1.between?(m3, m2)
[m2, m1, m3].sort
6.2 自定义==方法 #
ruby
class Person
attr_reader :name, :age
def initialize(name, age)
@name, @age = name, age
end
def ==(other)
name == other.name && age == other.age
end
def eql?(other)
self == other
end
def hash
[name, age].hash
end
end
p1 = Person.new("Alice", 30)
p2 = Person.new("Alice", 30)
p3 = Person.new("Bob", 25)
p1 == p2
p1 == p3
p1.eql?(p2)
七、类型检查 #
7.1 is_a? 和 kind_of? #
ruby
"hello".is_a?(String)
"hello".is_a?(Object)
42.is_a?(Integer)
42.is_a?(Numeric)
42.kind_of?(Integer)
7.2 instance_of? #
ruby
"hello".instance_of?(String)
"hello".instance_of?(Object)
42.instance_of?(Integer)
42.instance_of?(Numeric)
7.3 class比较 #
ruby
"hello".class == String
42.class == Integer
42.class < Numeric
八、nil检查 #
8.1 nil? #
ruby
nil.nil?
"".nil?
0.nil?
false.nil?
8.2 与false比较 #
ruby
nil == false
nil.eql?(false)
nil.equal?(false)
九、实用示例 #
9.1 版本比较 #
ruby
class Version
include Comparable
attr_reader :parts
def initialize(version)
@parts = version.split(".").map(&:to_i)
end
def <=>(other)
parts <=> other.parts
end
def to_s
parts.join(".")
end
end
v1 = Version.new("1.0.0")
v2 = Version.new("1.1.0")
v3 = Version.new("2.0.0")
v1 < v2
v2 < v3
v1.between?(v1, v3)
[v3, v1, v2].sort
9.2 范围比较 #
ruby
class TimeRange
include Comparable
attr_reader :start_time, :end_time
def initialize(start_time, end_time)
@start_time = start_time
@end_time = end_time
end
def <=>(other)
start_time <=> other.start_time
end
def overlaps?(other)
start_time < other.end_time && end_time > other.start_time
end
def contains?(time)
start_time <= time && end_time >= time
end
end
9.3 比较工具类 #
ruby
class Comparator
def self.compare(a, b)
case a <=> b
when -1 then "#{a} 小于 #{b}"
when 0 then "#{a} 等于 #{b}"
when 1 then "#{a} 大于 #{b}"
end
end
def self.min_max(*values)
values.minmax
end
def self.clamp(value, min, max)
[[value, min].max, max].min
end
end
Comparator.compare(1, 2)
Comparator.min_max(3, 1, 4, 1, 5)
Comparator.clamp(10, 1, 5)
十、总结 #
本章我们学习了:
- 相等比较:==、!=、eql?、equal?
- 大小比较:<、>、<=、>=、between?
- 太空船运算符:<=>、排序
- case相等:===、case语句
- 自定义比较:Comparable模块
- 类型检查:is_a?、kind_of?、instance_of?
接下来让我们学习Ruby的逻辑运算符!
最后更新:2026-03-27