Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Using reject in ruby to delete elements selectively from an array

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 519
    Comment on it

    1> To delete an element from an array, use reject as shown below:

    arr  = ['a','b','c','d','e']
    arr.reject! { |i| i.match('d')}
    Result :=> ["a", "b", "c", "e"]
    


    2> To delete particular keys from the array whose elements are also arrays, use reject as follows:

    Ex: If we have to delete the even keys from the given array, then use reject in a block as

    a = [ [ 1, "one"], [2, "two"], [3, "three"], [4, "four"] ]
    a.reject!{ |key,value| key%2==0}
    Result :=> [[1, "one"], [3, "three"]]
    

 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: