[Ruby] How to use any? Method

This is a personal memo.

By using the any? method, ** returns true if there is even one corresponding element in the target object **.

object.any?{ |variable|Conditional expression}


** If the block `{}` is omitted, true is returned if the value exists in the target object **.

Object.any?


## If there is a block

** ▼ true case **

true1


arr = [1, 2, 3]

arr.any?{|x| x < 2}
=> true

** ▼ false case **

false1


arr = [1, 2, 3]

arr.any?{|x| x > 10}
=> false

## Without blocks

** ▼ true case **

true1


arr = [1, 2, 3]

arr.any?
=> true

true2


arr = [nil, 2, nil]

arr.any?
=> true

** ▼ false case **

false1


arr3 = []

arr3.any?
=> false

false2


arr4 = [nil, nil, nil]

arr4.any?
=> false

Recommended Posts

[Ruby] How to use any? Method
How to use Ruby inject method
How to use Ruby return
Ruby: How to use cookies
[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
How to use the link_to method
How to use the include? method
How to use the form_with method
[Java] How to use join method
How to use Ruby on Rails
[Ruby on Rails] How to use CarrierWave
How to use submit method (Java Silver)
[Rails] How to use the map method
[Java] How to use the toString () method
Ruby length, size, count How to use
[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
[Rails] How to use helper method, confimartion
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use java.util.logging
How to use map
How to use Twitter4J
How to use active_hash! !!
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
How to use the replace () method (Java Silver)
[Java] How to use Map
How to use Chain API
[Java] How to use Map
How to use Priority Queuing
[Rails] How to use enum
How to use java Optional
How to use JUnit (beginner)
[Rails] How to use enum
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 Optional ②
[Java] How to use removeAll ()
How to use String [] args
[Java] How to use string.format
How to use rails join