Course Content

Course Content

FAQs

Use square brackets ([]) to build an array in a Ruby programme, and commas to separate the values you want to store.

Array#length(): length() is a method of the Array class that returns the number of elements in the array. The number of elements in the array is returned.

The simplest approach to print the contents of an array is to pass the array as a parameter to the puts or print method. On a separate line, each element is printed. The output is more readable when the examine method is used. The line outputs the array's string representation to the terminal.

Because the index of each item in an array maps to a specific memory address, which may be accessed directly without traversing the array, arrays allow for random access. Arrays just need to contain data, hence pointers to other memory addresses are unnecessary.

Array#unshift prepends an element to the beginning of an array, while Array#push appends an element to the end of an array in Ruby. The names of these approaches aren't immediately obvious. Prepend and append are aliases for the unshift and push methods in Active Support from Rails.

Share With Friend

Have a friend to whom you would want to share this course?

Download LearnVern App

App Preview Image
App QR Code Image
Code Scan or Download the app
Google Play Store
Apple App Store
598K+ Downloads
App Download Section Circle 1
4.57 Avg. Ratings
App Download Section Circle 2
15K+ Reviews
App Download Section Circle 3
  • Learn anywhere on the go
  • Get regular updates about your enrolled or new courses
  • Share content with your friends
  • Evaluate your progress through practice tests
  • No internet connection needed
  • Enroll for the webinar and join at the time of the webinar from anywhere