inspect()是Ruby中的內置方法,該方法返回具有特定結構值的字符串。
用法: struct_name.inspect()
參數:該函數不接受任何參數。
返回值:返回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"
相關用法
- Ruby Regexp inspect()用法及代碼示例
- Ruby Time inspect用法及代碼示例
- Ruby StringScanner inspect用法及代碼示例
- Ruby Rational inspect()用法及代碼示例
- Ruby Hash inspect()用法及代碼示例
- Ruby Symbol inspect用法及代碼示例
- Ruby Range inspect()用法及代碼示例
- Ruby Array class inspect()用法及代碼示例
- Ruby Struct eql?()用法及代碼示例
- Scala Struct ==用法及代碼示例
- Ruby Struct each()用法及代碼示例
- Ruby Struct values()用法及代碼示例
- Ruby Struct length()用法及代碼示例
- Ruby Struct to_a()用法及代碼示例
- Ruby Struct values_at()用法及代碼示例
注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 Ruby | Struct inspect() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。