📜  红宝石 |结构长度()函数

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

红宝石 |结构长度()函数

length()是 Ruby 中的一个内置方法,它返回 struct 中的成员数。

示例 1

# Ruby program for length method in struct 
    
# Include struct
Student = Struct.new(:name, :address, :dept)
  
# initialize values
detail = Student.new("Shizuka", "Washington", "CSE")
  
# length used
puts detail.length  

输出

3

示例 2

# Ruby program for length method in struct 
    
# Include struct
Student = Struct.new(:name, :address, :dept , :project)
  
# initialize values
detail = Student.new("Vishwa", "Hyderabad", "CSE" , "IOT")
  
# length used
puts detail.length  

输出

4