📜  红宝石 |结构 ==函数

📅  最后修改于: 2022-05-13 01:54:30.249000             🧑  作者: Mango

红宝石 |结构 ==函数

==是 Ruby 中的一个内置方法,如果 other 具有相同的结构子类并且具有相同的成员值,则返回 true。

示例 1

# Ruby program for == 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==struct2
puts struct1 == struct2

输出

true

示例 2

# Ruby program for == method in struct 
    
# Include struct
Employee = Struct.new(:company_name, :position, :zip)
  
#initialise struct
struct1  = Employee.new("GEEK", "INTERN", 12345)
struct2 = Employee.new("geeksforgeeks", "INTERN", 12345)
  
# Prints the value of struct1==struct2
puts struct1 == struct2

输出

false