Ruby length, size, count How to use

Introduction

It will be a memo for learning. In ruby, use the length method or size method to get the number of elements in the array.

Also, it is convenient to use the count method to specify the conditions and retrieve the elements of the array.

In this article, you will learn the basic usage.

length method

If you want to know the number of elements in an array, use the "length method" or "size method", but there are two types of length, the array class and the string class.

The length method of the array class is used for arrays and returns the number of elements in the array.

Also, the length method of the strign class returns the number of characters when used on a variable that contains a string.

array  = ["red","blue","yellow"]
str = "Engineers"
 
p array.length
p str.length

[Execution result]

3
6

size method

The size method returns exactly the same result as the length method.

array  = ["red","blue","yellow"]
str = "Engineers"
 
p array.size
p str.size

[Execution result]

3
6

count method

If you want to conditionally count the elements of an array, use the count method.

By specifying a condition in the argument, you can get only the number of elements that meet the condition.

If you want to know the number of elements of “red”

array = ["red","blue","yellow","red","green"]
p array.count("red")

[Execution result]

2

You can also specify a slightly more complicated condition by passing a block as an argument.

Let's count the number of elements whose remainder is 0, that is, the number of even numbers.

array = [1,2,2,2,3,3,4,5,]
p array.count{ |num| num % 2 == 0}

[Execution result]

4

Also, if you use the count method without specifying an argument, the number of elements is returned in the same way as length and size.

How to count the number of elements excluding nil (empty element) with count

I don't want to count nil if the array contains nil! In that case, specify the condition in the count method.

array = ["red","blue",nil,"yellow"]
p array.count{ |num| !num.nil? }

[Execution result]

3

Finally

This time I learned how to use the length, size and count methods. I would appreciate it if you could point out any mistakes.

Recommended Posts

Ruby length, size, count How to use
How to use Ruby return
Ruby: How to use cookies
How to use Ruby on Rails
[Ruby] How to use any? Method
How to use Ruby inject method
[Ruby on Rails] How to use CarrierWave
[Ruby] How to use slice for beginners
[Ruby on Rails] How to use redirect_to
[Ruby on Rails] How to use kaminari
Ruby: CSV :: How to use Table Note
How to use Map
How to use with_option
How to use fields_for
How to use java.util.logging
How to use map
How to use collection_select
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct
How to use hidden_field_tag
How to use TreeSet
[How to use label]
How to use identity
How to use hashes
How to use JUnit 5
How to use Dozer.mapper
How to use Gradle
How to use org.immutables
How to use java.util.stream.Collector
How to use VisualVM
How to use Map
[Ruby] How to use gsub method and sub method
[Ruby on Rails] How to use session method
[Ruby basics] How to use the slice method
[Ruby] How to use standard output in conditional branching
How to use Chain API
[Java] How to use Map
How to use Priority Queuing
[Ruby] Learn how to use odd? Even? And count the even and odd numbers in the array!
[Rails] How to use enum
How to use java Optional
How to use JUnit (beginner)
[Rails] How to use enum
[Ruby] How to use rbenv (version `x.x.x'is not installed)
How to use @Builder (Lombok)
[Swift] How to use UserDefaults
How to use java class
How to use Swift UIScrollView
[Ruby] How to comment out
How to use Big Decimal
[Java] How to use removeAll ()
How to use String [] args
[Java] How to use string.format
How to use rails join
How to use Java Map
Rbenv command to use Ruby
How to use dependent :: destroy
How to use Eclipse Debug_Shell
How to use Apache POI
[Rails] How to use validation