Ruby | Enumerable none?() function

The none?() of enumerable is an inbuilt method in Ruby returns a boolean value true if none of the objects in the enumerable satisfies the given condition, else it returns false. It compares all the elements with the pattern and returns true if none of them matches with the pattern.

Syntax enu.none? { |obj| block } or enu.none?(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 true or nil.



Return Value: It returns a boolean value.

Example #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby program for none? method in Enumerable
    
# Initialize an enumerable
enu1 = [10, 19, 18]   
    
# checks if all numbers are greater 
# than 4 or not 
res1 = enu1.none? { |num| num>4
  
# prints the result 
puts res1 
  
  
# checks if all numbers are greater 
# than 4 or not 
res2 = enu1.none? { |num| num>=20
  
# prints the result 
puts res2 

chevron_right


Output:

false
true

Example #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby program for none? method in Enumerable
    
# Initialize an enumerable
enu1 = [10, 19, 20]   
    
# Checks
res1 = enu1.none?(Numeric)
  
# prints the result 
puts res1 
  
# Initialize
enu2 = [nil, nil]
  
# Checks 
res2 = enu2.none? 
# prints the result 
puts res2 

chevron_right


Output:

false
true


My Personal Notes arrow_drop_up

The function of education is to teach one to think intensively and to think critically

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.