红宝石 |结构检查()函数
inspect()是 Ruby 中的一个内置方法,它返回一个带有特定结构值的字符串。
Syntax: struct_name.inspect()
Parameters: The function does not accepts any parameter.
Return Value: It returns the value of struct.
示例 1 :
# Ruby program for inspect method in struct
# Include struct
animals = Struct.new(:name, :speciality , :found_in)
# initialize values
detail = animals.new("labrador", "bark" , "Newfoundland")
# inspect used
puts detail.inspect
输出:
struct name="labrador", speciality="bark", found_in="Newfoundland"
示例 2 :
# Ruby program for inspect method in struct
# Include struct
animals = Struct.new(:name)
# initialize values
detail = animals.new("golden retriever")
# inspect used
puts detail.inspect
输出:
struct name="golden retriever"