Ruby Vector to_a()函数
to_a() 是Ruby中一个内置的方法,用于返回含有向量元素的数组。
语法 :vec1.to_a()
参数 :该函数不接受任何参数
返回值 :它返回含有向量元素的数组。
例子 1 :
# Ruby program for to_a() method in Vector
# Include matrix
require "matrix"
# Initialize the vector
vec1 = Vector[1, 2, 3]
# Prints the array
puts vec1.to_a()
输出:
1
2
3
例2 :
# Ruby program for to_a() method in Vector
# Include matrix
require "matrix"
# Initialize the vector
vec1 = Vector[1, 2, 3, 4, 5]
# Prints the array
puts vec1.to_a()
输出:
1
2
3
4
5