Skip to content
Related Articles

Related Articles

Improve Article

Ruby | Vector [] method

  • Last Updated : 07 Jan, 2020

The [] is an inbuilt method in Ruby returns the element at position x, indexing starts from 0.

Syntax: vec1[x]

Parameters: The function does not accepts any parameter.

Return Value: It returns the element at position x
.



Example 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]

Output:

5

Example 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]

Output:

3
My Personal Notes arrow_drop_up
Recommended Articles
Page :