Ruby | Enumerable all? function
The all?() of enumerable is an inbuilt method in Ruby returns a boolean value true if all the objects in the enumerable satisfies the given condition, else it returns false. If a pattern is given, it compares with the pattern, and returns true if all of them are equal to the given pattern, else it returns false.
Syntax enu.all? { |obj| block } or enu.all?(pattern)
Parameters: The function takes two types of parameters, one is the object and the block, while the other is the pattern. In case nothing is passed, it assumes to be default object and block which returns true if none of the objects are false or nil.
Return Value: It returns a boolean value.
Example #1::
enu1 = [ 10 , 19 , 18 ]
res1 = enu1.all? { |num| num> 4 }
puts res1
res2 = enu1.all? { |num| num>= 15 }
puts res2
|
Output:
true
false
Example 2:
enu1 = [ 10 , 19 , 20 ]
res1 = enu1.all?( Numeric )
puts res1
enu2 = [ nil , nil ]
res2 = enu2.all?
puts res2
|
Output:
true
false
Last Updated :
05 Dec, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...