红宝石 |向量[]方法
[]是 Ruby 中的一个内置方法,返回位置 x 处的元素,索引从 0 开始。
Syntax: vec1[x]
Parameters: The function does not accepts any parameter.
Return Value: It returns the element at position x
.
示例 1 :
#Ruby program for[] method in Vector
#Include matrix
require "matrix"
#Initialize the vector
vec1
= Vector[1, 5]
#Initialize the vector
x
= 1
#prints the new vector
puts vec1[x]
输出:
5
示例 2 :
#Ruby program for[] method in Vector
#Include matrix
require "matrix"
#Initialize the vector
vec1
= Vector[3, 6, 9]
#Initialize the vector
x
= 0
#prints the new vector
puts vec1[x]
输出:
3