
# 定义一个类
class Person
attr_accessor :name, :age
def initialize(name, age)
@name = name
@age = age
end
# 定义双等号方法
def ==(other)
return false unless other.is_a?(Person)
@name == other.name && @age == other.age
end
end
# 创建两个对象
person1 = Person.new("Alice", 30)
person2 = Person.new("Alice", 30)
person3 = Person.new("Bob", 25)
# 测试双等号
puts person1 == person2 # true
puts person1 == person3 # false
更多技术文章见公众号: 大城市小农民
1万+

被折叠的 条评论
为什么被折叠?



