Ruby | Vector component() function
The component() is an inbuilt method in Ruby returns element at index i in the vector. Indexing starts from zero.
Syntax: vec1.component(i)
Parameters: The function accepts a single parameter
Return Value: It returns element at index i in the vector
Example 1:
#Ruby program for component() method in Vector
#Include matrix
require "matrix"
#Initialize the vector
vec1
= Vector[1, 2]
i
= 1
#Prints element at index i
puts vec1.component(i)
|
Output:
2
Example 2:
#Ruby program for component() method in Vector
#Include matrix
require "matrix"
#Initialize the vector
vec1
= Vector[1, 2, 3]
i
= 2
#Prints element at index i
puts vec1.component(i)
|
Output:
3
Last Updated :
07 Jan, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...