Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Ruby Enumerators

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 141
    Comment on it

    Enumerator is a type of class which allows both the internal and external iteration.

     

    List Of Enumerable Methods in Ruby:

    • collect
    • map
    • each 
    • select

     

    Collect:

    It is similar to the map method and it allows us to iterate through the elements of the collection.

    For Example:

    x = [1,2,3,4]
    y = x.collect{|a| 2*a}
    puts y
    
    # y = [2,4,6,8]

     

    Map:

    It iterates through a collection of array , performs a specific action on that array and returns the array.

    For Example:

    x = [2,4,6,8]
    x.map do |y|
      y*2
    end
    
    # x = [4,8,12,16]

     

    Each:

    It returns all the elements of an array or hash.

    For Example:

    x = ["bike","cycle","car"]
    x.each do |y|
      puts y
    end
    
    
    # x = ["bike","cycle","car"]

     

    Select:

    It allows us to select elements from a group of elements.

    x = [1,2,3,4,5,6,7,8,9]
    x.select |y|
      y >= 3 && y <= 8
    end
    
    
    # x = [3,4,5,6,7,8]

 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: