红宝石 |构造 eql?()函数
eql?()是 Ruby 中的一个内置方法,如果 other 具有相同的 struct 子类并且具有相同的成员值,则返回 true。
Syntax: struct1.eql?(struct2)
Parameters: The function accepts no parameter.
Return Value: It returns boolean value true if both the given ranges are equal, else it returns false.
示例 1 :
# Ruby program for eql? method in struct
# Include struct
Employee = Struct.new(:company_name, :position, :zip)
#initialise struct
struct1 = Employee.new("GEEK", "INTERN", 12345)
struct2 = Employee.new("GEEK", "INTERN", 12345)
# Prints the value of struct1.eql?(struct2)
puts struct1.eql?(struct2)
输出:
true
示例 2 :
# Ruby program for eql? method in struct
# Include struct
Employee = Struct.new(:company_name, :position)
#initialise struct
struct1 = Employee.new("GEEK", "INTERN")
struct2 = Employee.new("Data structure", "INTERN")
# Prints the value of struct1.eql?(struct2)
puts struct1.eql?(struct2)
输出:
false