📜  红宝石 |构造 to_a()函数

📅  最后修改于: 2022-05-13 01:54:28.726000             🧑  作者: Mango

红宝石 |构造 to_a()函数

to_a()是 Ruby 中的一个内置方法,它返回一个包含特定结构值的数组。

示例 1

Ruby
# Ruby program for to_a  method in struct
   
# Include struct
animals = Struct.new(:name, :speciality , :found_in)
 
#initialize values
detail = animals.new("labrador", "bark" , "Newfoundland")
 
#print value 2
puts detail.to_a[2]


Ruby
# Ruby program for to_a method in struct
   
# Include struct
place = Struct.new(:name, :speciality)
 
#initialize values
detail = place.new("nagpur","orange")
 
#print value 1
puts detail.to_a[1]


输出

Newfoundland

示例 2

红宝石

# Ruby program for to_a method in struct
   
# Include struct
place = Struct.new(:name, :speciality)
 
#initialize values
detail = place.new("nagpur","orange")
 
#print value 1
puts detail.to_a[1]

输出

orange