• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Ruby / Using Ruby to Get the Length of Array

Using Ruby to Get the Length of Array

October 15, 2022 Leave a Comment

There are many ways we can use Ruby to get the length of an array. We can use the length method, the size method, or the count method to get the length of an array. Let’s take a look at using each one.

Let’s take a look at the length method first.

arr.length

And here is a simple example.

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]

puts numbers.length

#Output
9

The length property is straightforward and one of the easiest ways to get the length of an array. We can use another method that will return the same result as the length method, the size method.

arr.size

Let’s see the size method in action below with our same example.

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]

puts numbers.size

#Output
9

Finally, let’s look at the count method for finding the length of an array. This will work the same way as the length and size methods.

arr.count

Let’s see the count method in action below with our same example.

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]

puts numbers.count

#Output
9

When working with arrays, it can be useful to be able to easily calculate the array length and size of an array.

To get the length of an array in Ruby, we can use either the length, size, or count methods.

Let’s see these methods used on a bunch of different arrays.

numbersArray = [32,1,0,9,23,430,1000]
stringsArray = ["This","an","array","of","strings"]
mixedArray = ["Hello","",nil,"null",true,234,"23"]
arr1 = []
arr2 = [""]

puts numbersArray.length
puts numbersArray.size
puts numbersArray.count

puts stringsArray.length
puts stringsArray.size
puts stringsArray.count

puts mixedArray.length
puts mixedArray.size
puts mixedArray.count

puts arr1.length
puts arr1.size
puts arr1.count

puts arr2.length
puts arr2.size
puts arr2.count 

#Output
7
7
7
5
5
5
7
7
7
0
0
0
1
1
1

Hopefully this article has been useful for you to learn how to use Ruby to get the length of array.

Other Articles You'll Also Like:

  • 1.  Using Ruby to Print to Console
  • 2.  Using Ruby to Convert Array to String
  • 3.  Ruby Ceiling – Find the Ceiling of a Number with the ceil Method
  • 4.  Ruby has_key Method – Check if Hash Has Key
  • 5.  Ruby puts vs print
  • 6.  Using Ruby to Convert String to Boolean
  • 7.  Ruby String Comparison
  • 8.  How to Print Without Newline in Ruby
  • 9.  Ruby Infinite Loop
  • 10.  Using Ruby to Reverse String

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy