Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Ruby Enumerables: (all, any, none, find)

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 749
    Comment on it

    Ruby provides a number of enumerable methods that we can pass on collections or array for performing searching, sorting etc operations.

    Here I will be discussing with all, any, none and find methods:


    a) all?

    In this every member of the collection is passed to a block to check whether it satisfies the given condition and returns true if each of the member satisfies the conditions and returns false when any of the member doesn't satisfy the condition. i.e:

     

      > [3, 5, 7, 9].all? {|n| n%2 == 0 } ## To check for any even number
      => false
    
      > [3, 5, 8, 9].all? {|n| n%2 == 0 } ## To check for any even number
      => false
    
      > [2, 4, 8].all? {|n| n%2 == 0 } ## To check for any even number
      => true
    
    

     

    b) any?

    In this case it also passes one by one each member of the collection to block to check whether it satisfies the given condition or not, if any member satisfies the condition, it returns true otherwise false

      > [3, 5, 7, 9].any? {|n| n > 10 } ## To check whether any number is greater than 10
      => false
    
      > [2, 4, 8, 12].any? {|n| n > 10 } ## To check whether any number is greater than 10
      => true

     

    c) none?

    It is just the opposite of all? method. It returns true if none of the member matches the condition specified in the block, and returns false if any of the member satisfies the condition
     

     > [3, 5, 7, 9].none? {|n| n > 10 } ## To check whether any number is greater than 10
      => true
    
     > [2, 4, 8, 12].none? {|n| n > 10 } ## To check whether any number is greater than 10
      => false

     

    d) find and find_all

    find also passes each element to the condition specified in the block and returns the first element that specifies that condition and find_all returns an array of all the element that specifies the given condition. If no element specifies it return nil or empty array respectively
     

      > [3, 5, 7, 9].find {|n| n > 10 } ## To get the first number greater than 10
      => nil
    
      > [3, 5, 7, 9].find_all {|n| n > 10 } ## To get all the numbers greater than 10
      => []
    
      > [3, 5, 7, 9, 12, 15, 18].find {|n| n > 10 } ## To get the first number greater than 10
      => 12
    
      > [3, 5, 7, 9, 12, 15, 18].find_all {|n| n > 10 } ## To get all the numbers greater than 10
      => [12, 15, 18]

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: