红宝石 |结构尺寸()函数
size()是 Ruby 中的一个内置方法,它返回结构中存在的成员总数。
Syntax: struct_name.size()
Parameters: The function does not accepts any parameter.
Return Value: It returns the integer specifying the members of struct.
示例 1 :
# Ruby program for size method in struct
# Include struct
Places = Struct.new(:name, :speciality)
#initialize values
detail = Places.new("Nagpur", "oranges")
# size used
puts detail.size
输出:
2
示例 2 :
# Ruby program for size method in struct
# Include struct
animals = Struct.new(:name, :speciality , :found_in)
# initialize values
detail = animals.new("labrador", "bark" , "Newfoundland")
# size used
puts detail.size
输出:
3