Skip to content
Related Articles

Related Articles

Improve Article

Ruby | Vector component() function

  • Last Updated : 07 Jan, 2020

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
My Personal Notes arrow_drop_up
Recommended Articles
Page :