红宝石 |结构成员()函数
members()是 Ruby 中的一个内置方法,它返回一个包含结构成员的符号数组。
Syntax: struct_name.members()
Parameters: The function does not accepts any parameter.
Return Value: It returns the array with struct members.
示例 1 :
# Ruby program for members method in struct
# Include struct
Student = Struct.new(:name, :address, :dept , :project)
#initialize values
detail = Student.new("Vishwa", "Hyderabad", "CSE" , "IOT")
# members used
puts detail.members
输出:
name
address
dept
project
示例 2 :
# Ruby program for members method in struct
# Include struct
Movie = Struct.new(:name, :imdbrating)
# initialize values
detail = Movie.new("3idiots", 5)
# members used
puts detail.members
输出:
name
imdbrating