Ruby 结构体 length()函数
length() 是Ruby中的一个内置方法,用于返回结构中成员的数量。
语法 :struct_name.length()
参数 :该函数不接受任何参数。
返回值 :它返回结构中成员的数量。
示例 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