Open In App

Ruby | Vector component() function

Improve
Improve
Like Article
Like
Save
Share
Report

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
Previous
Next
Share your thoughts in the comments
Similar Reads